WordPress.org

Ready to get started?Download WordPress

Forums

Trying to access wp_dropdown_pages() output in plugin from dashboard (1 post)

  1. marionkdit
    Member
    Posted 4 months ago #

    Hi! I'm trying to figure out how to update the instance with the output from the wp_dropdown_menu() function from inside the dashboard. Here's my code. Any ideas? I need to pull the page id, update the instance, and then be able to retrieve the id user-side so I can create a customized featured page by passing the id into WP functions.

    <?php
    /*
    Plugin Name: Mardit's Featured Page Plugin
    Plugin URI: http://mardit.com/plugins/mardit_featured_page.php
    Description: This plugin will allow a user to choose a page to display in a featured page widget with a customizable learn more button and excerpt.
    Version: 1.0
    Author: Mario Keeney-Ditolla
    Author URI: http://mardit.com
    License:
    */
    
    class mardit_featured_page_plugin extends WP_Widget {
    
    	// constructor
    	function __construct(){
    		parent::__construct(
    			'mardit_featured_page_plugin',
    			__('Mardit Featured Page', 'text_domain'),
    			array( 'description' => __('Mardit Featured Page Plugin', 'text_domain'),));
    
    		}
    
    	// display widget
    	function widget($args, $instance) {
    
    	   // these are the widget options
    	   $title = apply_filters('widget_title', $instance['title']);
    	   $page_id = $instance['page_id'];
    
    	   echo $args['before_widget'];
    	   // Display the widget
    	   echo '<div class="widget-text wp_widget_plugin_box">';
    
    	   // Check if title is set
    	   if ( $title ) {
    	      echo $args['before_title'] . 'Featured Page' . $args['after_title'];
    	   }
    
    	   // Check if text is set
    	   echo $instance['page_id'];
    	   echo '</div>';
    	   echo $args['after_widget'];
    	}
    
    	// widget form creation
    	function form($instance) {
    
    	// Check values
    	if( $instance) {
    	     $title = esc_attr($instance['title']);
    	     $page_id = esc_attr($instance['page_id']);
    	} else {
    	     $title = '';
    	     $page_id = '';
    	}
    ?>
    
    		<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 esc_attr( $title ); ?>" />
    		</p>
    
    		<p>
    			<label for="<?php echo $this->get_field_id('page_id');?>"><?php _e('Page ID');?></label>
    			<?php wp_dropdown_pages(array(
    			    'id' => 'page_id',
    			    'name' => $this->get_field_name('page_id'),
    			    'selected' => $instance['page_id'],
    			));?>
    		</p>
    
    <?php
    
    	}//end form
    
    	// widget update
    	function update($new_instance, $old_instance) {
    		$instance = array();
    		//fields
    		$instance['title'] = strip_tags($new_instance['title']);
    		$instance['page_id'] = strip_tags($new_instance['page_id']);
    		return instance;
    	}//end update
    }//end class
    // register widget
    function register_mardit_widget() {
    	register_widget('mardit_featured_page_plugin');
    }
    
    add_action('widgets_init', create_function('', 'return register_widget("mardit_featured_page_plugin");'));
    
    ?>

Reply

You must log in to post.

About this Topic