Display Recently Viewed Post with Visitors IP

In one of my previous post, I showed how to add Dashboard widget using simple PHP code and with little help from WordPress hooks. Today, I will go little further and show you guys how we can take the advantage of custom made Dashboard widget to track the visitors behaviors. In other words, we will track recently viewed posts on your site. We will track both the visit time and the user's IP addresses.

This function would follow a simple method of recording every single https:// request made to your posts. One drawback of such function is, it would track both the BOTs and the regular users interaction. I did this willingly to keep the snippet smaller. If you are interested to dig deep, feel free to do so. This is just a sample snippet to show what can be done with little snippet in WordPress.

Recently Viewed PostsThis is how your Dashboard Widget would look like.

First thing we would do is to create a simple tracking code that would record the IP address of the user and the time of their visit. Here is the snippet.

// tracking code
if (get_post_type($post->ID) == 'post') { 

Simply copy this snippet and paste it on your theme's single.php page. Update the page as usual. Now, as you can see from the snippet, we are simply tracking blog post.

Now, we need to create our function to register our dashboard widget and than hook it up with the another function that would grab the data from our database. Here is the snippet.

// register the dashboard widget to display data
function add_recently_viewed_widget() {  
      'Recently Viewed', 
      'recently_viewed_posts'); }
   add_action('wp_dashboard_setup', 'add_recently_viewed_widget');
// retrieve the data and display it
function recently_viewed_posts(){
   global $post;
   query_posts( array(
      'post_type' => 'post',
      'posts_per_page' => 5,
      'meta_key' => 'post_last_viewed',
      'orderby' => 'meta_value',
      'order' => 'DESC'));	
if(have_posts()) : 
   echo '<ul>';
while(have_posts()) : the_post(); 
   $ip = get_post_meta($post->ID,'post_visitor_ip',true);
   $time = get_post_meta($post->ID,'post_last_viewed',true);
   echo '<li><a href="'.get_permalink($post->ID).'">';
   echo get_the_title().'</a>';
   echo '<div style="color:#888888;"><small><strong>';
   echo human_time_diff(strtotime($time), current_time('timestamp'));
   echo ' ago &#183; ';
   echo date('M jS, g:i a', strtotime($time)).' &#183; ip: ';
   echo $ip.'</strong></small></div>';
   echo '</li>';
   echo '</ul>';
   wp_reset_query(); }

The first portion of this snippet should not be something new to you, if you were following my previous posts. I already wrote about it. On second portion though, we were simply querying posts with "post_last_viewed" meta key. If any post has the value of that meta key that would also mean it has already been viewed by someone. It also should have the IP address of the visitor as it was recorded at the same time.

Simply, copy and paste this snippet on your theme's functions.php file and update it. Don't forget to enable your widget from the "screen option" menu (top right hand corner of the screen).

Custom Post Type Support

Just in case if you want to track your custom post type as well, replace the "post" word with your post type name on my tracking code and add this snippet on your single-cpt_name.php file.

Now, we also need to query custom post type along with regular post. To do that simply change the "post_type" parameter's value with an array like following from the "recently_viewed_posts" function.

'post_type' => array('post','cpt_name'),

This should allow our function to query both the blog post and the custom post type. That's all.

Like always, I am not going to explain this snippet because it would make this post really long. However, you can post any possible question that may have regarding this snippet.

Note: We must remember that "Pages" are not going to be our main concern rather its the blog posts that we would be tracking. Also remember that, all these information would be saved on your site's database, so if you have thousands of post, I wouldn't personally recommend to use this as it might overwhelm your database if you get huge traffic on your site. If you think your server is powerful enough to handle lots of reading and writing on your database go for it.


Commenting is disabled.