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
- /**
- * Plugin Name: BAW Post Count views
- * Plugin URI: http://www.tutorialstag.com
- * Description: Count views for post and pages, shortcode [post_view] available
- * Version: 1.0
- * Author: Julio
- * Author URI: http://www.boiteaweb.fr
- * License: GPLv2
- **/
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
- function baw_count_views()
- {
- if( is_singular() ) {
- global $post;
- $count = get_post_meta( $post->ID, '_count-views', true );
- $count++;
- update_post_meta( $post->ID, '_count-views', $count );
- }
- }
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
- 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
- function baw_count_views_sc( $atts, $content = null )
- {
- global $post;
- ), $atts));
- if( (int)$id > 0 ) {
- $count = (int)get_post_meta( $id, '_count-views', true );
- if( $count > 1000 ) { $count = $count / 1000 . 'K'; }
- return $count;
- }
- return '';
- }
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
- add_shortcode( 'post_view', 'baw_count_views_sc' );
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 ID “123“.
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:
- <?php
- echo '<p>';
- echo 'This post have been displayed ';
- echo do_shortcode( '[post_view]' );
- echo ' times. Thank you !';
- echo '</p>';
- ?>
c) You add add a hook to “the_content” to avoid modifing the template theme:
- function baw_post_view_in_content( $content )
- {
- $content .= '<p>';
- $content .= 'This post have been displayed ';
- $content .= do_shortcode( '[post_view]' );
- $content .= ' times. Thank you !';
- $content .= '</p>';
- return $content;
- }
- 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”!











Hi Julio,
I think it’s a good way to show social proof to your visitors. Maybe I’m gonna add this function in my theme in the coming weeks, I don’t know where to place it yet.
It’s nice to read you here
Alex
Hey Alex
Thank you
If you wait for part2, i’ll add stats and widget :]
So a little bit louder, but includeing stats if you like it (i don’t haha) !
See you !
Hi Julio,
In my little test, on a local host, the code :
function baw_post_view_in_content(){
echo '';
echo 'This post have been displayed ';
echo do_shortcode( '[post_view]' );
echo ' times. Thank you !';
echo '';
}
add_action( 'the_content', 'baw_post_view_in_content' );
replaces the content by this single line.
I think a filter is more appropriate… what do you think about that ?
function baw_post_view_in_content($content){
$content .= '';
$content .= __('This post have been displayed ');
$content .= do_shortcode( '[post_view]' );
$content .= __(' times. Thank you !');
$content .= '';
return $content;
}
add_filter( 'the_content', 'baw_post_view_in_content' );
Thanks !
See ya
I totally agree, i’m wrong.
I started to write this “echo” code to give a theme example, then i changed my mind and add a hook+function.
But, a return is better AND do not erase the content too
Thank you again !
Nice article. I just have one question. How well does this play with WordPress cache plugins?
Hello
I don’t know, i did not try. Remember, this is not a plugin, this is a tutorial, kind of “how to make a plugin”. I decided to not just display a “hello world” text, so i did this simple “plugin”
Is it possible ? Not sure.
I’ll try to test with a cache plugin, but i don’t know how to count views via caching files
Cache plugins will not affect the count updates to the database but at the front end it will take some time to show updated count depending on the flush time you set in the cache plugin.
Ok good to know, thank you Kannan!
How i can add this Plugin On my HTML Site.
Hello, you can not, this is a WordPress plugin, sorry !
My dear this is not WordPress Plugin.This is HTML for Blogger.Butt thanks.i have use your post on my blogger.Great Work Keep it up.
This IS a WordPress Plugin. The last version is here: http://baw.li/pvc