During building widget, I am facing some PHP error problem.The following code shows error: All codes are below:
<?php
/**
* Plugin Name: Strawberry Popular Post Widgets
* Plugin URI: http://arifhossin.com/
* Description: A widget that shows popular posts ( Specified by comment post view ).
* Version: 1.0
* Author: Arif Hossin
* Author URI: http://www.arifhossin.com
*
*/
add_action( 'widgets_init', 'personalblog_popular_posts_widget' );
function personalblog_popular_posts_widget() {
register_widget( 'Personalblog_Popular_Posts' );
}
class Personalblog_Popular_Posts extends WP_Widget{
// Initialize the widget
public function __construct() {
parent::WP_Widget(
'personalblog_popular_posts_widget', esc_html__('Strawberry: Popular Posts Widget','strawberry'), array('description' => esc_html__('Strawberry:A widget that shows Polupar Posts', 'strawberry')));
}
// Output of the widget
public function widget( $args, $instance ) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
$posts_order_by = $instance['posts_order_by'];
$poular_posts_time_range = $instance['poular_posts_time_range'];
$num_fetch = $instance['num_fetch'];
// Opening of widget
echo $args['before_widget'];
// Open of title tag
if( !empty($title) ){
echo $args['before_title'] . $title . $args['after_title'];
}
$args = array(
'post_type' => 'post',
'posts_per_page' => $num_fetch,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'post_status' => 'publish',
'orderby' => $posts_order_by,
'order' => 'DESC',
'date_query' => array(
array(
'after' => $posts_time_range,
),
),
);
$posts_query = new WP_Query($args); ?>
<?php if ( $posts_query->have_posts() ) { ?>
<div class="pb-recent-posts-widget-section clearfix">
<ul class="pb-recent-post-widget-list">
<?php while ( $posts_query->have_posts() ) : $posts_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class('widget-post-entry clearfix'); ?>>
<figure class="pb-recent-post-widget-thumbnail-section">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_post_thumbnail( 'widget-post-image' ); ?>
</a>
</figure>
<div class="pb-recent-post-widget-section">
<h2 class="pb-recent-post-widget-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php if( pb_theme_options('enable_widget_meta') == '1' ) { ?>
<div class="pb-recent-post-widget-meta clearfix">
<ul>
<?php if( pb_theme_options('widget_meta_date') == '1' ) { ?>
<li><i class="fa fa-clock-o"></i><span><?php echo get_the_date(); ?></span></li>
<?php } ?>
<?php if( pb_theme_options('widget_meta_comments') == '1' ) { ?>
<li><i class="fa fa-comments">
<span>
<?php printf( _nx( '', '%1$s', 'comments title', get_comments_number(), 'personalblog' ), number_format_i18n( get_comments_number() ) ); ?>
</span>
</i>
</li>
<?php } ?>
</ul>
<ul>
<?php if( pb_theme_options('widget_meta_category') == '1' ) { ?>
<li><p class="category"><i class="fa fa-folder-open"> </i> <?php the_category(', '); ?></p></li>
<?php } ?>
<?php if( pb_theme_options('widget_meta_likes') == '1' ) { ?>
<li><?php echo getPostLikeLink( get_the_ID() )?> </li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php }
wp_reset_postdata();
// Closing of widget
echo $after_widget;
}
// Widget Form
public function form( $instance ) {
$title = isset($instance['title'])? $instance['title']: 'Popular Posts';
$posts_order_by = isset($instance['posts_order_by'])? $instance['posts_order_by']: '';
$poular_posts_time_range = isset($instance['poular_posts_time_range'])? $instance['poular_posts_time_range']: '';
$num_fetch = isset($instance['num_fetch'])? $instance['num_fetch']: 5;
?>
<p>
<label for="<?php echo esc_attr ($this->get_field_id('title')); ?>"><?php esc_html_e('Title :', 'strawberry'); ?></label>
<input class="widefat" id="<?php echo esc_attr ($this->get_field_id('title')); ?>" name="<?php echo esc_attr ( $this->get_field_name('title')); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo esc_attr ($this->get_field_id('posts_order_by')); ?>"><?php esc_html_e( 'Order by:', 'strawberry' ); ?></label>
<select name="<?php echo esc_attr ($this->get_field_name('posts_order_by')); ?>" id="<?php echo esc_attr ($this->get_field_id('posts_order_by')); ?>" class="widefat">
<option value="meta_value_num"<?php selected( $instance['posts_order_by'], 'post_views_count' ); ?>><?php esc_html_e( 'View Count', 'strawberry' ); ?></option>
<option value="comment_count"<?php selected( $instance['posts_order_by'], 'comment_count' ); ?>><?php esc_html_e( 'Most commented', 'strawberry' ); ?></option>
</select>
</p>
<p>
<label for="<?php echo esc_attr ($this->get_field_id('poular_posts_time_range')); ?>"><?php esc_html_e( 'Time Range:', 'strawberry' ); ?></label>
<select name="<?php echo esc_attr ( $this->get_field_name('poular_posts_time_range')); ?>" id="<?php echo esc_attr ( $this->get_field_id('poular_posts_time_range')); ?>" class="widefat">
<option value="0"<?php selected( $instance['poular_posts_time_range'], '0' ); ?>><?php esc_html_e( 'All time', 'strawberry' ); ?></option>
<option value="1 year ago"<?php selected( $instance['poular_posts_time_range'], '1 year ago' ); ?>><?php _e( 'This year', 'strawberry' ); ?></option>
<option value="1 month ago"<?php selected( $instance['poular_posts_time_range'], '1 month ago' ); ?>><?php esc_html_e( 'This month', 'strawberry' ); ?></option>
<option value="1 week ago"<?php selected( $instance['poular_posts_time_range'], '1 week ago' ); ?>><?php esc_html_e( 'This week', 'strawberry' ); ?></option>
<option value="1 day ago"<?php selected( $instance['poular_posts_time_range'], '1 day ago' ); ?>><?php esc_html_e( 'Past 24 hours', 'strawberry' ); ?></option>
</select>
</p>
<p>
<label for="<?php echo esc_attr ($this->get_field_id('num_fetch')); ?>"><?php esc_html_e('Num Fetch :', 'strawberry'); ?></label>
<input class="widefat" id="<?php echo esc_attr ($this->get_field_id('num_fetch')); ?>" name="<?php echo esc_attr ($this->get_field_name('num_fetch')); ?>" type="text" value="<?php echo $num_fetch; ?>" />
</p>
<?php
}
// Update the widget
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['posts_order_by'] = $new_instance['posts_order_by'];
$instance['poular_posts_time_range'] =$new_instance['poular_posts_time_range'];
$instance['num_fetch'] = $new_instance['num_fetch'];
return $instance;
}
}
?>
Error: 1)Undefined index: posts_order_by-126 line
<option value="meta_value_num"<?php selected( $instance['posts_order_by'], 'post_views_count' ); ?>><?php esc_html_e( 'View Count', 'strawberry' ); ?></option>
2)Undefined index: posts_order_by-127 line
<option value="comment_count"<?php selected( $instance['posts_order_by'], 'comment_count' ); ?>><?php esc_html_e( 'Most commented', 'strawberry' ); ?></option>
3)Undefined index: poular_posts_time_range-134 line
<option value="0"<?php selected( $instance['poular_posts_time_range'], '0' ); ?>><?php esc_html_e( 'All time', 'strawberry' ); ?></option>
4)Undefined index: poular_posts_time_range-135 line
<option value="1 year ago"<?php selected( $instance['poular_posts_time_range'], '1 year ago' ); ?>><?php esc_html_e( 'This year', 'strawberry' ); ?></option>
Thanks in advance.