Add Post Views Counter In WordPress Posts Without Plugin

There are many different ways you can get involved with WordPress development. Even if you do not have slightest idea of what is php coding all about or what is my Sql database but still if you have word press website and that too self-hosted then you for sure get more and more understanding about how to build and manage a website and that includes prominent part of coding. During the process of development of website, all that includes theme development. During the process of theme development I always advocate for using TwentyTwelve theme as the basis for many customization and many such editing can be easily done with his theme. Most of such coding are available with internet and simple search within internet sphere could lead you to reach about this.

Post views

Post views

Today’s article is about adding post views in wordpress posts without plugin. In the initial years of my blogging career, I have been very much interested in blogging with Google’s blogging platform Blogger. It provides an awesome level of site statistics and a counter at the front of website to attach so that every user who visits your website exactly counter of total visitors could be well known. After turning into self-hosted wordpress platform four years back I do realize for everything you do with a website you need plugin.

Most of plugin developers are from the third person and definitely still the time you are not double sure about their credentials you should not like to install such plugins. Most of these plugins do provide gigantic form of liabilities for hosting and thus makes website loads slowly. Primary focus of Google and other web indexing services are that of web page landing times and for this it is essential to understand the proficiency of the web page so that it should land fast and smoothly. There has been many articles about this which day in and out has been advocating of using lesser plugins and that also should be kept in mind while building a web page.

WordPress is very versatile blogging platform and it offers everything to user beginning from their own ways to customize it. It runs with php and converted into html while loading on user’s web browser and it loads database and website simultaneously. Of course in modern times there has been many such ways of distribution of content such as distribution through, database and though it is a very complicated process, but editing of these files and adding customization through its hosting panel becomes easier.

Most of these code optimization changes with the different themes. It is better to have TwentyTwelve them as the process of learning so that everything can be done with complete ease and then you could move to some other theme later on. I always intended to focus more on doing and adding on simple codes to themes files so that ultimately. All these functions which come in the form of plugins do ultimately, can be done easily with simple codes. Most of these plugins are formed of codes which are included in the form of simple files developed through third parties.

So, why not we as developers does this all by ourselves. In this article, we should be finding out how to insert codes into hosting files of themes so that ultimately, we could see post views without plugins. Most of these plugins tend to do a lot more that what it should have been intended to do and that is why it is important to use code snippets so that this should be fast and there should not be any further chance of snooping of website with such plugins.
Following code does what you need is to show posts views and nothing else. It is important to edit theme files from hosting and then reaching to cPanel. Do not edit theme files from wordpress administration as this could temporary down your site.

Always back up files so that at any stage you got it wrong. Then you could upload that back up files and replace it with the current files. In this code snippet, we would have two forms of codes. The first form of code to be inserted within functions dot php and which counts post views and then the second form of code for displaying post views, with single dot php at the end of the single post prior to author information. The additional code is for display of post views.

CODE SNIPPET: functions.php

// function to display number of posts.
function getPostViews($postID){
 $count_key = 'post_views_count';
 $count = get_post_meta($postID, $count_key, true);
 if($count==''){
 delete_post_meta($postID, $count_key);
 add_post_meta($postID, $count_key, '0');
 return "0 View";
 }
 return $count.' Views';
}

// function to count views.


function setPostViews($postID) {
 $count_key = 'post_views_count';
 $count = get_post_meta($postID, $count_key, true);
 if($count==''){
 $count = 0;
 delete_post_meta($postID, $count_key);
 add_post_meta($postID, $count_key, '0');
 }else{
 $count++;
 update_post_meta($postID, $count_key, $count);
 }
}

// Remove issues with prefetching adding extra views.
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

 

First part of function is to display the number of posts. It counts post views in term of getting post views through its meta content and then in return it removes post metadata and add post metadata and the return comes with post views count. In the second part of this code that should be included in functions.php files are about to count views. The post views counts begin with zero and slowly with further visibility of website. In the third part of functions.php code which aims to remove issues with prefethcing which might add extra views, which ultimately, provide the real time and realistic views of website. Add this code at the end of functions.php files.

Following code snippet to be added to single.php files. It should be added within the loop. How to find it within the loop. Open single.php files. Search for

<?php endwhile;

It is the end of the loop. Add the following new codes, just above the code. End while is the end of the loop and when the code to be inserted, within loop it should be inserted before end while statement.

CODE SNIPPET: single.php

<?php setPostViews(get_the_ID()); ?>
 
 <?php echo getPostViews(get_the_ID()); ?>

This code snippet, works normally, and it is advisable to use in child themes so that the process of counting of posts continues even if there is updated to your current theme. It is important to put codes from cPanel hosting so that ultimately, whenever something goes wrong, you can upload the back up php files. Back up php files so that, if at all something goes wrong then you could easily retrieve it.

*A few days ago. I read a post on wpsnipp.com which discussed ‘How to Track post views without a plugin using post meta’. I’d never hear of this before, but I was intrigued, so I read on.*