Writing a Post Views Count Plugin: Part 1

Today we will learn to create a “Post Views Count” plugin, including a shortcode to use it.
This plugin can be useful to display visitors how many times this post have been read. Also, you can easily use it in any post or page, or in your theme template or with a function hook.

Let’s see how to do this :

Step 1: The special plugin header

  1. /**
  2. * Plugin Name: BAW Post Count views
  3. * Plugin URI: http://www.tutorialstag.com
  4. * Description: Count views for post and pages, shortcode [post_view] available
  5. * Version: 1.0
  6. * Author: Julio
  7. * Author URI: http://www.boiteaweb.fr
  8. * License: GPLv2
  9. **/

Now the goal is to increment a counter each time a post, custom post or page is viewed.
I use a post meta named “_count-views“. It starts with a “_” to avoid it to be changed manually in the custom field meta box. If you want to change it manually, just name it “count-views” here and everywhere in this tutorial.

Step 2: The “save the data” function

  1. function baw_count_views()
  2. {
  3. if( is_singular() ) {
  4. global $post;
  5. $count = get_post_meta( $post->ID, '_count-views', true );
  6. $count++;
  7. update_post_meta( $post->ID, '_count-views', $count );
  8. }
  9. }
This function check if a post, custom post, page or attachment is loaded with “is_singular()“, then we grab the “_count-views” data from DB, we increment it with “$count++” and we update the post meta.
Remark: i added a “global $post” because i need the post ID, do not forget to globalize $post when you need it!

Step 3: The action

I use “wp_head“, this hook is executed once per page, so i can hook my own function on it

  1. add_action( 'wp_head', 'baw_count_views' );

Ok, now the data is saved, but how to use it ?
I created a shortcode named [post_view]

Step 4: The shortcode function

  1. function baw_count_views_sc( $atts, $content = null )
  2. {
  3. global $post;
  4. extract(shortcode_atts(array(
  5. "id" => isset( $post->ID ) ? (int)$post->ID : 0,
  6. ), $atts));
  7. if( (int)$id > 0 ) {
  8. $count = (int)get_post_meta( $id, '_count-views', true );
  9. if( $count > 1000 ) { $count = $count / 1000 . 'K'; }
  10. return $count;
  11. }
  12. return '';
  13. }

I globalize $post again, i extract the data from the shortcode, here i need the post ID, if not assigned, the actual post ID will be used.
If this ID is correct, i grab the meta data from the DB and returns it.

Step 5: The shortcode declaration

  1. add_shortcode( 'post_view', 'baw_count_views_sc' );
Remark: The 1st param is the shortcode name.

Step 6: How to use it

a) [post_view] will display the counter for the actual post, custom post or page.
b) [post_view id="123"] will display the counter for the post/page ID123“.

Step 7: How to display it

a) You can add this shortcode in any post or page.
b) You can add this shortcode in your theme template, open single.php and page.php, find “the_content()“, below this, write:

  1. <?php
  2. echo '<p>';
  3. echo 'This post have been displayed ';
  4. echo do_shortcode( '[post_view]' );
  5. echo ' times. Thank you !';
  6. echo '</p>';
  7. ?>

c) You add add a hook to “the_content” to avoid modifing the template theme:
  1. function baw_post_view_in_content( $content )
  2. {
  3. $content .= '<p>';
  4. $content .= 'This post have been displayed ';
  5. $content .= do_shortcode( '[post_view]' );
  6. $content .= ' times. Thank you !';
  7. $content .= '</p>';
  8. return $content;
  9. }
  10. add_action( 'the_content', 'baw_post_view_in_content' );

In the next tutorial, i’ll improve this plugin to add stats per day/week/month/year and add a widget to display “Most Viewed Posts”!