Popular posts for “Count per Day” WordPress plugin

popularpostsLeaseWeb Labs runs on WordPress and we are using the “Count per Day” plugin to keep track of our visitors. One big advantage of Count per Day is that it does not use cookies, but counts the amount of unique IP addresses per day. These are reported as “visitors”, while the amount of page views are reported as “reads”. In our experience this is a very simple and reliable way of measuring the visitors on our site. Especially when you enable the option “Cache: Count visits with ajax” in the Settings, because that will prevent bots from showing up in the statistics.

Some posts are better and thus more popular than other posts. When people visit your site for a certain post, they may also be interested in other popular posts. This is why we wanted the most popular posts listed in the sidebar (as you can see on the left). Since the popularity of posts is changing all the time and we did not feel like maintaining a static list of posts in menu. Count per Day has a the shortcode “CPD_MOST_VISITED_POSTS” to generate this list for you. Unfortunately this code generates the list including numbers and a heading. This is not exactly the way we like it and thus we created a WordPress widget for our (and your) convenience.

The widget can be simply dragged into a widget area and allows you to:

  • Set the title for the list
  • Set the amount of days to take into account
  • Set the maximum number of items in the list
  • Show/hide the counts for the selected period
  • Show/hide the header that explains the counts

We contacted Tom Braider (the author of this fantastic plugin) and we hope he will include the widget in a next version. Until then you can install the widget by adding the following code to “widgets.php” using the theme editor:

if (isset($count_per_day)) {

// 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'); });

}
Share

WordPress “User Groups” support for “Email Users” plugin

Most sports clubs have websites nowadays. Such a website needs a secured area where you can access personal information of other members of the sports club in order to be able to contact them. For my sports club, I am exploring the possible plugins that can be added to WordPress to make it “sports club ready”.

We use the “Members” plugin to allow users with the “Subscriber” role to access private pages. Then we installed the “User Groups” plugin to allow for club members to be in more than one group. We need them to be in more than one group, because we want to have a group of active members, groups for individual teams, and groups for things like management of the sports canteen.

Then we wanted to be able to allow members to easily email each other or these groups. We found the excellent  “Email Users” plugin for that. It allows registered users to send emails to other users or groups. Unfortunately the groups that “Email Users” supports are the roles and not the groups that are configured through the “User Groups” plugin.

That is why I wrote a patch to make the WordPress “Email Users” plugin support the groups from the “User Groups” plugin. I have added the “Group Taxonomy” option and if you set it to “user-group” you enable the connection between the plugins. By providing another value it could also be bound to other user group plugins that are like the “User Groups” plugin based on “terms” within a “taxonomy”.

taxonomy
Figure 1: The “Email Users” plugin has a new “Group Taxonomy” option that is set to “user-group” to support the “User Groups” plugin.

Hopefully the patch (that I also posted on the support forum) gets accepted in some new version so that anyone can enjoy the powerful combination of these plugins.

          diff -C 5 email-users/email_users_group_mail_form.php email-users-new/email_users_group_mail_form.php
          *** email-users/email_users_group_mail_form.php	2013-04-11 15:08:34.000000000 +0200
          --- email-users-new/email_users_group_mail_form.php	2013-06-23 12:04:25.679559080 +0200
          ***************
          *** 100,109 ****
          --- 100,111 ----
            			<td>
            				<select id="send_roles" name="send_roles[]" multiple="multiple" size="8" style="width: 654px; height: 250px;">
            				<?php
                                if ($group_mode == 'meta')
            					    $roles = mailusers_get_group_meta_filters($user_ID, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META);
          +                     elseif ($group_mode == 'term')
          + 					    $roles = mailusers_get_groups($user_ID, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META);
                                else
            					    $roles = mailusers_get_roles($user_ID, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META);
            					foreach ($roles as $key => $value) {
            				?>
            					<option value="<?php echo $key; ?>"	<?php
          diff -C 5 email-users/email_users_options_form.php email-users-new/email_users_options_form.php
          *** email-users/email_users_options_form.php	2013-04-11 15:08:34.000000000 +0200
          --- email-users-new/email_users_options_form.php	2013-06-23 11:57:20.545450942 +0200
          ***************
          *** 125,134 ****
          --- 125,142 ----
            			<?php _e('Exclude sender from email recipient list.', MAILUSERS_I18N_DOMAIN); ?><br/>
            		</td>
            	</tr>
            	<tr>
            		<th scope="row" valign="top">
          +             <label for="group_taxonomy"><?php _e('Group Taxonomy', MAILUSERS_I18N_DOMAIN); ?></th>
          + 		<td>
          + 			<input type="text" name="mailusers_group_taxonomy" style="width: 235px;"
          + 				value="<?php echo format_to_edit(mailusers_get_group_taxonomy()); ?>"
          + 				size="80" id="group_taxonomy"/><br/>&nbsp;<?php _e('A taxonomy used to get terms from that represent user groups.', MAILUSERS_I18N_DOMAIN); ?></td>
          + 	</tr>
          + 	<tr>
          + 		<th scope="row" valign="top">
                        <label for="from_sender_name_override"><?php _e('From Sender<br/>Name Override', MAILUSERS_I18N_DOMAIN); ?></th>
            		<td>
            			<input type="text" name="mailusers_from_sender_name_override" style="width: 235px;"
            				value="<?php echo format_to_edit(mailusers_get_from_sender_name_override()); ?>"
            				size="80" id="from_sender_name_override"/><br/>&nbsp;<?php _e('A name that can be used in place of the logged in user\'s name when sending email or notifications.', MAILUSERS_I18N_DOMAIN); ?></td>
          diff -C 5 email-users/email-users.php email-users-new/email-users.php
          *** email-users/email-users.php	2013-04-11 15:08:34.000000000 +0200
          --- email-users-new/email-users.php	2013-06-23 12:12:25.461938202 +0200
          ***************
          *** 94,104 ****
            		// Mail User - Default setting for User Control
            		'mailusers_default_user_control' => 'true',
            		// Mail User - Default setting for Short Code Processing
            		'mailusers_shortcode_processing' => 'false',
            		// Mail User - Default setting for Short Code Processing
          ! 		'mailusers_from_sender_exclude' => 'true'
            	) ;

                if (array_key_exists($option, $default_plugin_settings))
                    return $default_plugin_settings[$option] ;
                else
          --- 94,106 ----
            		// Mail User - Default setting for User Control
            		'mailusers_default_user_control' => 'true',
            		// Mail User - Default setting for Short Code Processing
            		'mailusers_shortcode_processing' => 'false',
            		// Mail User - Default setting for Short Code Processing
          ! 		'mailusers_from_sender_exclude' => 'true',
          ! 		// Mail User - Taxonomy for User Group
          ! 		'mailusers_group_taxonomy' => ''
            	) ;

                if (array_key_exists($option, $default_plugin_settings))
                    return $default_plugin_settings[$option] ;
                else
          ***************
          *** 435,445 ****
             * Wrapper for the email users send to group menu
             */
            function mailusers_send_to_group_page()
            {
                global $mailusers_send_to_group_mode ;
          !     $mailusers_send_to_group_mode = 'role' ;
                require_once('email_users_send_group_mail.php') ;
            }

            /**
             * Wrapper for the email users send to group custom meta menu
          --- 437,451 ----
             * Wrapper for the email users send to group menu
             */
            function mailusers_send_to_group_page()
            {
                global $mailusers_send_to_group_mode ;
          ! 	if (mailusers_get_group_taxonomy()) {
          ! 		$mailusers_send_to_group_mode = 'term';
          ! 	} else {
          ! 		$mailusers_send_to_group_mode = 'role' ;
          ! 	}
                require_once('email_users_send_group_mail.php') ;
            }

            /**
             * Wrapper for the email users send to group custom meta menu
          ***************
          *** 616,625 ****
          --- 622,632 ----
                register_setting('email_users', 'mailusers_max_bcc_recipients') ;
                register_setting('email_users', 'mailusers_user_settings_table_rows') ;
                register_setting('email_users', 'mailusers_shortcode_processing') ;
                register_setting('email_users', 'mailusers_from_sender_exclude') ;
                register_setting('email_users', 'mailusers_from_sender_name_override') ;
          +     register_setting('email_users', 'mailusers_group_taxonomy') ;
                register_setting('email_users',
                    'mailusers_from_sender_address_override', 'mailusers_from_sender_address_override_validate') ;
                register_setting('email_users',
                    'mailusers_send_bounces_to_address_override', 'mailusers_send_bounces_to_address_override_validate') ;
                register_setting('email_users', 'mailusers_version') ;
          ***************
          *** 729,738 ****
          --- 736,752 ----
            function mailusers_get_from_sender_name_override() {
            	return get_option( 'mailusers_from_sender_name_override' );
            }

            /**
          +  * Wrapper for the group taxonomy option
          +  */
          + function mailusers_get_group_taxonomy() {
          + 	return get_option( 'mailusers_group_taxonomy' );
          + }
          +
          + /**
             * Wrapper for the from sender name override option
             */
            function mailusers_update_from_sender_name_override( $from_sender_name_override ) {
            	return update_option( 'mailusers_from_sender_name_override', $from_sender_name_override );
            }
          ***************
          *** 1001,1010 ****
          --- 1015,1042 ----

            	return $roles;
            }

            /**
          +  * Get the users based on groups
          +  * $meta_filter can be '', MAILUSERS_ACCEPT_NOTIFICATION_USER_META, or MAILUSERS_ACCEPT_MASS_EMAIL_USER_META
          +  */
          + function mailusers_get_groups($exclude_id='', $meta_filter = '') {
          + 	$roles = array();
          +
          + 	$terms = get_terms('user-group', array('hide_empty' => false));
          + 	foreach ( $terms as $term ) {
          + 		$users_in_role = mailusers_get_recipients_from_terms(array($term->term_id), $exclude_id, $meta_filter);
          + 		if (!empty($users_in_role)) {
          + 			$roles[$term->term_id]=$term->name;
          + 		}
          + 	}
          + 	return $roles;
          + }
          +
          +
          + /**
             * Get the users based on group custom meta filters
             * $meta_filter can be '', MAILUSERS_ACCEPT_NOTIFICATION_USER_META, or MAILUSERS_ACCEPT_MASS_EMAIL_USER_META
             */
            function mailusers_get_group_meta_filters( $exclude_id='', $meta_filter = '') {
            	$filters = array();
          ***************
          *** 1051,1060 ****
          --- 1083,1104 ----

                return $users ;
            }

            /**
          +  * Get the users given a term or an array of terms
          +  * $meta_filter can be '', MAILUSERS_ACCEPT_NOTIFICATION_USER_META, or MAILUSERS_ACCEPT_MASS_EMAIL_USER_META
          +  */
          + function mailusers_get_recipients_from_terms($terms, $exclude_id='', $meta_filter = '') {
          +
          + 	$ids = get_objects_in_term($terms, 'user-group');
          +
          + 	return mailusers_get_recipients_from_ids($ids, $exclude_id, $meta_filter);
          + }
          +
          +
          + /**
             * Get the users given the existance of a custom meta filter
             * $meta_filter can be '', MAILUSERS_ACCEPT_NOTIFICATION_USER_META, or MAILUSERS_ACCEPT_MASS_EMAIL_USER_META
             */
            function mailusers_get_recipients_from_custom_meta_filter( $ids, $exclude_id='', $meta_filter='', $meta_value='', $meta_compare='=') {

          diff -C 5 email-users/email_users_send_group_mail.php email-users-new/email_users_send_group_mail.php
          *** email-users/email_users_send_group_mail.php	2013-04-11 15:08:34.000000000 +0200
          --- email-users-new/email_users_send_group_mail.php	2013-06-23 12:11:20.665616866 +0200
          ***************
          *** 126,135 ****
          --- 126,137 ----
            	<?php
            		// Fetch users
            		// --
                    if ($group_mode == 'meta')
            		    $recipients = mailusers_get_recipients_from_custom_meta_filters($send_filters, $exclude_id, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META);
          +         elseif ($group_mode == 'term')
          + 		    $recipients = mailusers_get_recipients_from_terms($send_roles, $exclude_id, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META);
                    else
            		    $recipients = mailusers_get_recipients_from_roles($send_roles, $exclude_id, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META);

            		if (empty($recipients)) {
            	?>
          Common subdirectories: email-users/images and email-users-new/images
          Common subdirectories: email-users/languages and email-users-new/languages
Share