PopularPostsWidget for Count-Per-Day
-
Add to the file “counter.php”:
include_once($cpd_path.'counter-widget.php');
Next add a file called “counter-widget.php” containing:
<?php // Creating the widget class PopularPostsWidget extends WP_Widget { function __construct() { parent::__construct( // Base ID of your widget 'cpd_popular_posts_widget', // Widget name will appear in UI 'Popular Posts', // Widget description array( 'description' => 'Count-Per-Day Popular Posts')); } // Creating widget front-end // This is where the action happens public function widget( $args, $instance ) { $title = apply_filters( 'widget_title', $instance['title'] ); $days = $instance['days' ]+0; $limit = $instance['limit']+0; $head = $instance['head' ]?true:false; $count = $instance['count']?true:false; // before and after widget arguments are defined by themes echo $args['before_widget']; if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } // This is where you run the code and display the output global $count_per_day; $html = $count_per_day->getMostVisitedPosts($days, $limit, 1, 1, 1); if (!$head ) $html = preg_replace('/<small>[^<]*<\/small>/','',$html); if (!$count) $html = preg_replace('/<b>[^<]*<\/b>/','',$html); echo $html; echo $args['after_widget']; } // Widget Backend public function form( $instance ) { $title = isset($instance['title'])?$instance['title']:'Popular Posts'; $days = isset($instance['days' ])?$instance['days' ]:'7'; $limit = isset($instance['limit'])?$instance['limit']:'10'; $head = isset($instance['head' ])?$instance['head' ]:false; $count = isset($instance['count'])?$instance['count']:false; // Widget admin form ?> <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( 'days' ); ?>"><?php _e( 'Days:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'days' ); ?>" name="<?php echo $this->get_field_name( 'days' ); ?>" type="text" value="<?php echo esc_attr( $days ); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Limit:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="text" value="<?php echo esc_attr( $limit ); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'head' ); ?>"><?php _e( 'Show header:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'head' ); ?>" name="<?php echo $this->get_field_name( 'head' ); ?>" type="checkbox" <?php echo $head?'checked="checked"':'' ?> /> </p> <p> <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show counters:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" type="checkbox" <?php echo $count?'checked="checked"':'' ?> /> </p> <?php } // Updating widget replacing old instances with new public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; $instance['days' ] = ( ! empty( $new_instance['days' ] ) ) ? strip_tags( $new_instance['days' ] ) : ''; $instance['limit'] = ( ! empty( $new_instance['limit'] ) ) ? strip_tags( $new_instance['limit'] ) : ''; $instance['head' ] = ( ! empty( $new_instance['head' ] ) ) ? strip_tags( $new_instance['head' ] ) : ''; $instance['count'] = ( ! empty( $new_instance['count'] ) ) ? strip_tags( $new_instance['count'] ) : ''; return $instance; } } // Class PopularPostsWidget ends here // Register and load the widget add_action( 'widgets_init', function (){ register_widget('PopularPostsWidget'); });
It serves my needs. Can it be added to the next version?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘PopularPostsWidget for Count-Per-Day’ is closed to new replies.