How to Track and Display Post Views in WordPress Using a Custom Plugin

Views: 2

Tracking post views in WordPress is a great way to measure engagement on your blog. Instead of relying on third-party analytics tools, you can create a custom WordPress plugin to track and display post views efficiently. In this guide, we will walk through the process of building a plugin that:

  • Tracks post views each time a visitor views a post.
  • Displays the view count before the post content.
  • Adds a column in the WordPress admin panel to see the views for each post.
  • Includes a shortcode to manually place the view count anywhere.

Step 1: Create the Plugin File

First, create a new folder named post-view-tracker in your WordPress wp-content/plugins/ directory. Inside this folder, create a PHP file named post-view-tracker.php and open it in a text editor.

Add the following plugin header:

<?php
/**
 * Plugin Name: Post View Tracker
 * Plugin URI:  https://codoplex.com
 * Description: Tracks and displays post views dynamically using 'the_content' filter.
 * Version: 1.0
 * Author: Junaid Hassan
 * Author URI: https://codoplex.com
 * License: GPL2
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly

This registers the plugin with WordPress.

Step 2: Track Post Views

We need to track post views when a visitor reads a blog post. We’ll store the view count in the post meta.

// Function to track post views
function pvt_track_post_views() {
    if (is_single()) {
        global $post;

        if (!isset($post->ID)) return;

        // Avoid counting views from admins
        if (current_user_can('edit_posts')) return;

        $post_id = $post->ID;
        $views = get_post_meta($post_id, '_post_views', true);
        $views = ($views) ? (int) $views + 1 : 1;

        update_post_meta($post_id, '_post_views', $views);
    }
}
add_action('wp', 'pvt_track_post_views');

Step 3: Display Post Views Before Content

Now, let’s modify the_content filter to prepend the post views before the post content.

// Function to get post view count
function pvt_get_post_views($post_id) {
    $views = get_post_meta($post_id, '_post_views', true);
    return $views ? $views : 0;
}

// Function to prepend post views to the content
function pvt_prepend_views_to_content($content) {
    if (is_single()) {
        global $post;
        if (!isset($post->ID)) return $content;

        $views = pvt_get_post_views($post->ID);
        $views_html = "<p class='post-views' style='font-size: 14px; color: #555;'>Views: {$views}</p>";

        return $views_html . $content;
    }
    return $content;
}
add_filter('the_content', 'pvt_prepend_views_to_content');

Step 4: Create a Shortcode for Custom Placement

If you want to manually place the view count inside a post or widget, use this shortcode:

// Shortcode to manually display post views anywhere
function pvt_post_views_shortcode() {
    global $post;
    if (!isset($post->ID)) return '';

    $views = pvt_get_post_views($post->ID);
    return "<span class='post-views'>Views: {$views}</span>";
}
add_shortcode('post_view_count', 'pvt_post_views_shortcode');

Use Views: 2 anywhere inside a post, page, or widget to display the view count.

Step 5: Show Views in the WordPress Admin Panel

To display the post view count in the WordPress Posts list, add the following code:

// Add view count column in the admin post list
function pvt_add_views_column($columns) {
    $columns['post_views'] = 'Views';
    return $columns;
}
add_filter('manage_posts_columns', 'pvt_add_views_column');

function pvt_display_views_column($column, $post_id) {
    if ($column == 'post_views') {
        echo pvt_get_post_views($post_id);
    }
}
add_action('manage_posts_custom_column', 'pvt_display_views_column', 10, 2);

This will add a “Views” column in WP Admin > Posts, showing the view count for each post.

Step 6: Activate the Plugin

  1. Save your file as post-view-tracker.php inside the post-view-tracker folder.
  2. Go to WordPress Dashboard > Plugins.
  3. Find “Post View Tracker” and click Activate.

Now, your post views will be tracked and displayed automatically!

Conclusion

This simple yet powerful Post View Tracker Plugin allows you to track and display post views dynamically in WordPress without modifying theme files. With features like automatic tracking, dynamic display, shortcode support, and admin panel integration, it provides an efficient way to measure engagement on your blog posts.

💡 Want to customize the view display? Modify the pvt_prepend_views_to_content() function with custom HTML/CSS styling.

If you found this tutorial helpful, share it with fellow WordPress users and let me know if you need any enhancements! 🚀

Leave a Comment