Sidebar widget to show popular post not working?

This is the code to display popular post on front end, however the problem is the data is not saving from the back end. Like i want to show only 3 popular posts on front-end so i select the option 3 but its not saving. However when i pass the static value for $popularpostcount then this code works echo $this->popularPosts($popularpostcount); so i believe the problem is only saving the selected option.

I am new to plugin development so please let me know what mistake i am making while the development.

<?php
/*
Plugin Name: Popular Post Widget
Plugin URI: http://demo.test.com
Author: Swapnesh Kumar Sinha
Description: This is a plugin to show popular posts in sidebar as a widget.
Version: 1.0
Author URI: http://swapneshsinha.wordpress.com
*/



class PopularPostWidget extends WP_Widget {

public function __construct() {
    //Widget actual processes
    $widget_options = array( 'Description' => 'Show Popular Posts' );
    parent::__construct( 'PopularPostWidget', 'Popular Post Widget', $widget_options); 
}

public function form( $instance ) {
    //outputs the options form on admin
$instance = wp_parse_args( (array) $instance, array( 'popularpostcount' => '' ) );
$popularpostcount = $instance['popularpostcount'];  
echo $popularpostcount;
echo "Number of post to show "; 
echo "<select name='popularpostcount' id='popularpostcount'>";
    for( $i =1; $i<=5; $i++ )
    {
        if ( $i == $instance['popularpostcount'] )
        echo "<option value='".$i."' selected>".$i."</option>";
        else
        echo "<option value='".$i."'>".$i."</option>";
    } 
echo "</select>";
}

public function update( $new_instance, $old_instance ) {
    //processes widget options to be saved
    $instance = $old_instance;
    $instance['popularpostcount'] = $new_instance['popularpostcount'];
    return $instance;
}

public function widget( $args ,  $instance ) {
    //outputs the content of widget
    extract($args);
    $popularpostcount = apply_filters('widget_title', $instance['popularpostcount']);
    echo  "<h2>Most Popular Posts</h2>";
    echo  "<ul>";
    echo $this->popularPosts($popularpostcount);
    echo  "</ul>";
}


public function popularPosts($num)
{
     global $wpdb;

    $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");

    foreach ($posts as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $title = $post->post_title;
        $count = $post->comment_count;

        if ($count != 0) {
            $popular .= '<li>';
            $popular .= '<a href="' . get_permalink($id) . '" title="">' . $title.'</a> '." - ".$count;
            $popular .= '</li>';
        }
    }
    return $popular;
}


}

add_action( 'widgets_init' , create_function('', 'register_widget( "popularpostwidget" );' ) );

Related posts

Leave a Reply

2 comments

  1. Replace this line in form function:

    echo "<select name='popularpostcount' id='popularpostcount'>";
    

    with:

    echo "<select name='".$this->get_field_name('popularpostcount')."' id='".$this->get_field_name('popularpostcount')."'>";
    
  2. Note: in addition to Kilan’s answer, I would also recommend using the WordPress selected() function:

    echo "<select name='" . $this->get_field_name('popularpostcount') . "' id='popularpostcount'>";
        for( $i =1; $i<=5; $i++ )
        {
            echo "<option " . selected( $i == $instance['popularpostcount'] ) . " value='" . $i . "'>".$i."</option>";
        } 
    echo "</select>";