• I’m writing a custom widget for our site to display some chosen posts. In the admin part, I’ve got a multiple select box to let the admin choose multiple posts by their name. This is working fine, but when I select a few different posts and save, nothing is getting saved.

    Can anyone shed any light on this?

    Here is my code…

    <?php
    /*
    Plugin Name: Hot Topics
    Plugin URI: https://www.weddingideasmag.com
    Description: Use this widget to choose an array of posts snippets to show
    Version: 1.0)
    Author: James Payne
    Author URI: https://www.bluntcreative.co.uk
    License: GPL2
    */
    
    class HotTopics extends WP_Widget {
    
    	// constructor
    	function HotTopics() {
    		$widget_ops = array( 'name' => 'Hot Topics','classname' => 'widget-hot-topics', 'description' => __( "Use this widget to choose an array of posts snippets to show in the sidebar." ) );
    		$this->WP_Widget( 'hottopics', __('Hot Topics'), $widget_ops);
    	}
    
    	// widget form creation
    	function form($instance) {
    		// Check values
    		if( $instance) {
    			$select = esc_attr($instance['select']); // Added
    		} else {
    			 $select ='';
    		}
    		?>
    
    		<select multiple="multiple" name="<?php echo $this->get_field_name('select'); ?>[]" id="<?php echo $this->get_field_id('select'); ?>" class="widefat" size="15" style="margin-bottom:15px;">
    			<?php
    			$args = array( 'offset'=> 1, 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 200, 'post_status' => 'publish' );
    
    			// The Query
    			query_posts( $args );
    
    			// The Loop
    			while ( have_posts() ) : the_post();
    
    			$title = get_the_title();
    			?>
    				<option value="<?php echo get_the_ID();?>" class="hot-topic" <?php $select == $title ? ' selected="selected"' : '';?> style="margin-bottom:3px;">
    			    	<?php echo $title;?>
    			    </option>
    				<?php
    			endwhile;
    
    			// Reset Query
    			wp_reset_query();
    			?>
    		</select>
    
    		<?php
    	}
    
    	// update widget
    	function update($new_instance, $old_instance) {
    	      $instance = $old_instance;
    	      // Fields
    	      $instance['select'] = strip_tags($new_instance['select']);
    	     return $instance;
    	}
    
    	// widget display
    	function widget($args, $instance) {
    		/* ... */
    		echo 'tetst';
    	}
    }
    
    // register widget
    add_action('widgets_init', create_function('', 'return register_widget("HotTopics");'));
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello sir ( wedideas )

    I am sandeep ,

    Sir i am requesting you if you have solved this problem then please provide me this code sir If possible , I really baldly need this. My E-mail id is ( [email protected] )

    I am very thanking full to you.

    Thread Starter wedideas

    (@wedideas)

    Hi Sandeep, here is the working code I’m using…

    <?php
    /*
    Plugin Name: Hot Topics
    Plugin URI: https://www.weddingideasmag.com
    Description: Use this widget to choose an array of posts snippets to show
    Version: 1.0)
    Author: James Payne
    Author URI: https://www.bluntcreative.co.uk
    License: GPL2
    */
    
    // register widget
    add_action( 'widgets_init', function() {
        register_widget( 'HotTopics' );
    });
    
    class HotTopics extends WP_Widget {
    
    ///////////////////////////
    // Initialise the widget //
    ///////////////////////////	
    
        function HotTopics() {
            $this->WP_Widget(
                'hottopics',
                __('Hot Topics'),
                array(
                    'name' => 'Hot Topics',
                    'classname' => 'widget-hot-topics',
                    'description' => __( "Use this widget to choose an array of posts snippets to show in the sidebar." )
                )
            );
        }
    
    	// The admin form for the widget
    
    ///////////////////////////
    // Setup the form fields //
    ///////////////////////////
    
        function form( $instance )
        {
            if( $instance ) {
    			$title = $instance['title'];
                $select = $instance['select'];
    		} else {
    			$title ='';
                $select ='';
    		} ?>
    
    		<p>
    			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:', 'wp_widget_plugin'); ?></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>
    
    	<?php
    
    		// Populate the select list with the post titles
            $get_posts = get_posts( array(
                'offset'=> 1,
                'orderby' => 'title',
                'order' => 'ASC',
                'posts_per_page' => -1,
                'post_status' => 'publish'
            ));
    
    		// If there are posts, populate the select
            if( $get_posts ) {
    
    			echo '<p>There are <strong>' . count($get_posts) . '</strong> posts in the database. Choose the hot topics from the choices below&hellip;</p>';?>
    
    			<label for="<?php echo $this->get_field_id('select'); ?>"><?php _e('Posts:', 'wp_widget_plugin'); ?></label>
    
    			<?php
                printf (
                    '<select multiple="multiple" name="%s[]" id="%s" class="widefat" size="15" style="margin-bottom:10px">',
                    $this->get_field_name('select'),
                    $this->get_field_id('select')
                );
    
    			// Each individual option
                foreach( $get_posts as $post )
                {
                    printf(
                        '<option value="%s" class="hot-topic" %s style="margin-bottom:3px;">%s</option>',
                        $post->ID,
                        in_array( $post->ID, $select) ? 'selected="selected"' : '',
                        $post->post_title
                    );
                }
    
                echo '</select>';
    
            } else {
    
    			// No posts were found
                echo 'No posts found :(';
    
    		} // END: if( $get_posts ) {
    
        } // END: function form( $instance ) 
    
    //////////////////////////////////////////////////////////////////
    // The update function to insert the chosen values in to the db //
    //////////////////////////////////////////////////////////////////
    
        function update( $new_instance, $old_instance )
        {
            $instance = $old_instance;
            $instance['select'] = esc_sql( $new_instance['select'] );
    		$instance['title'] = esc_sql( $new_instance['title']);
            return $instance;
        }
    
    /////////////////////////////////////////
    // The front end display of the widget //
    /////////////////////////////////////////
    
    	function widget($args, $instance) {
    	   extract( $args );
    	   // these are the widget options
    	   $title = apply_filters('widget_title', $instance['title']);
    	   $text = $instance['select'];
    	   echo $before_widget;
    		   // Display the widget
    		   echo '<div class="widget-text wp_widget_plugin_box">';
    
    		   // Check if title is set
    		   if ( $title ) {
    			  echo $before_title . $title . $after_title;
    		   }
    
    		   // Check if text is set
    			if( $text ) { ?>
    
    				<div class="sidebar-posts">
    
    					<?php query_posts(array('post_type' => 'post', 'post__in' => $text, 'orderby' => 'rand')); ?>
    
    					<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    						<div class="sidebar-posts-entry">
    
    							<?php if ( has_post_thumbnail() ) {?>
    
    								<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(75,75),array('class' => 'thumbnail'));?></a>
    
    							<?php } ?>
    
    							<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    
    							<p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,12).'&hellip; '; ?></p>
    
    						</div>
    
    						<div style="clear:both;" class="clearBoth"></div>
    
    					<?php endwhile; endif; ?>
    
    				</div> <?php // END: <div class="sidebar-posts">
    
    				// Reset Query
    				wp_reset_query();
    
    			 // print_r($text);
    		   }
    		   echo '</div>';
    	   echo $after_widget;
    	}
    
    } // END: class HotTopics
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom widget remember multiple select options’ is closed to new replies.