Example:
<?php class PP_Fields_Widget extends WP_Widget { function __construct() { // Instantiate the parent object $widget_ops = array( 'classname' => 'widget_title_fields_entries', 'description' => esc_attr__('Post & Page Field Widget - Created by deshisoft', 'eye-theme') ); $control_ops = array( 'width' => 275 ); parent::__construct( false, 'Page & Post Field Widget', $widget_ops, $control_ops ); } function widget( $args, $instance ) { extract( $args ); // Widget output $title = apply_filters('widget_title', $instance['title']); // Page Select $page_url = apply_filters('widget_page', $instance['page_url']); // Post Select $post_url = apply_filters('widget_post', $instance['post_url']); // Output echo $before_widget; echo '<div style="border:1px solid #ddd; background:#f6f6f6;padding:15px;">'; echo esc_attr($title); echo '<br/>'; echo esc_attr($page_url); echo '<br/>'; echo esc_attr($post_url); echo '</div>'; echo $after_widget; } function update( $new_instance, $old_instance ) { // Save widget options $instance = array(); $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['page_url'] = strip_tags( $new_instance['page_url'] ); $instance['post_url'] = strip_tags( $new_instance['post_url'] ); return $instance; } function form( $instance ) { // Output admin widget options form $title = $instance['title']; $page_url = $instance['page_url']; $post_url = $instance['post_url']; ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> <!-- Page URL Select --> <p><label for="<?php echo $this->get_field_id('page_url'); ?>">PAGE URL: <select class="widefat" id="<?php echo $this->get_field_id('page_url'); ?>" name="<?php echo $this->get_field_name('page_url'); ?>"> <option value=""> <?php echo esc_attr( __( 'Select Page' ) ); ?></option> <?php $pages = get_pages(); foreach ( $pages as $page ) { $option = '<option '. (get_page_link( $page->ID ) == attribute_escape($page_url) ? "selected='selected'":"").'value="' . get_page_link( $page->ID ) . '">'; $option .= $page->post_title; $option .= '</option>'; echo $option; } ?> </select> </label> </p> <!-- Post URL Select --> <p><label for="<?php echo $this->get_field_id('post_url'); ?>">POST URL: <select class="widefat" id="<?php echo $this->get_field_id('post_url'); ?>" name="<?php echo $this->get_field_name('post_url'); ?>"> <option value=""> <?php echo esc_attr( __( 'Select Post' ) ); ?></option> <?php $posts = get_posts(); foreach ( $posts as $post ) { $option = '<option '. (get_page_link( $post->ID ) == attribute_escape($post_url) ? "selected='selected'":"").'value="' . get_page_link( $post->ID ) . '">'; $option .= $post->post_title; $option .= '</option>'; echo $option; } ?> </select> </label> </p> <?php } } function pp_field_register_widgets() { register_widget( 'PP_Fields_Widget' ); } add_action( 'widgets_init', 'pp_field_register_widgets' ); ?>
Theme Folder -> function.php
Add this line:
require get_template_directory() . '/inc/page_post_widget.php'; // inc folder name depend on your theme.