Track and Display Unique Post Views by IP in WordPress with a Custom Function

Estimated Reading Time: 4 min

Rate this post

Here’s a well-optimized WordPress post that explains how to use the be_smart post view counter function. It’s SEO-friendly and ready for use with Yoast SEO.


Track Unique Post Views by IP in WordPress with a Custom Function

Are you looking for a simple and effective way to track how many unique views your WordPress posts get? With our custom be_smart post view function, you can easily count post views based on IP addresses and display them directly on your posts. This ensures each user only counts once, providing you with more accurate statistics.

🔥 Why Track Post Views by IP?

Tracking unique views by IP helps you:

  • Get accurate post view counts without counting repeated visits from the same user.
  • Measure the real reach of your content.
  • Increase user engagement by displaying post view counts.

How to Set Up Unique Post Views in WordPress

Step 1: Add the PHP Code

To enable unique post view tracking, add this custom code to your theme’s functions.php file or a custom plugin:

// Function to get the user's IP address
function be_smart_get_user_ip() {
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        return $_SERVER['REMOTE_ADDR'];
    }
}

// Function to track views based on IP address
function be_smart_track_post_views($post_id) {
    if (is_single()) {
        $ip_address = be_smart_get_user_ip();
        $views = get_post_meta($post_id, 'be_smart_post_views', true);
        $ip_list = get_post_meta($post_id, 'be_smart_ip_list', true);

        if (!$views) {
            $views = 0;
        }

        if (!$ip_list) {
            $ip_list = [];
        }

        // Track unique views
        if (!in_array($ip_address, $ip_list)) {
            $ip_list[] = $ip_address;
            update_post_meta($post_id, 'be_smart_ip_list', $ip_list);
            update_post_meta($post_id, 'be_smart_post_views', ++$views);
        }
    }
}

// Hook to count views when a post is viewed
function be_smart_set_post_views() {
    if (is_single()) {
        global $post;
        be_smart_track_post_views($post->ID);
    }
}
add_action('wp_head', 'be_smart_set_post_views');

// Function to display the view count
function be_smart_get_post_views($post_id) {
    $views = get_post_meta($post_id, 'be_smart_post_views', true);
    return $views ? $views : '0';
}

// Shortcode to display views in the post
function be_smart_post_views_shortcode() {
    global $post;
    $views = be_smart_get_post_views($post->ID);
    return "Views: " . $views;
}
add_shortcode('be_smart_views', 'be_smart_post_views_shortcode');

Step 2: Display Views on Your Posts

You can show the number of unique views in two ways:

1️⃣ Automatically in Templates
If you want to display the post views in your theme template, add this line of code inside single.php or wherever you want the views to appear:

echo be_smart_get_post_views(get_the_ID());

2️⃣ Using a Shortcode
Add the following shortcode inside your post or page editor:

[be_smart_views]

🚀 Benefits of Using This Method

  • Tracks unique views accurately based on IP addresses.
  • Lightweight and doesn’t require extra plugins.
  • Can be customized to reset views, exclude bots, or track specific post types.

🛠 Customizations You Might Need

  • Exclude specific IPs (e.g., your own).
  • Reset views manually from the WordPress dashboard.
  • Track views for specific custom post types.

📊 Final Thoughts

Adding a custom post view counter by IP is a great way to gain insight into how your content is performing without relying on third-party plugins. Whether you’re running a blog, a news site, or a niche content hub, tracking views effectively can help you improve your content strategy and grow your audience.

Try this simple code today, and watch your analytics become more accurate than ever!


Did you find this tutorial helpful? Leave a comment below, and don’t forget to share this guide with fellow WordPress enthusiasts!


Tags: #postviews, #trackviews, #uniqueviews, #IP-basedviews

Photo of author

Flora

Track and Display Unique Post Views by IP in WordPress with a Custom Function

Published

I am Flora, the publisher and founder of *Be-Smart*, a platform dedicated to sharing insights and inspiration for living a fulfilling life. With a strong background in the web, my goal is to empower people to genuinely recognize and celebrate admirable actions big or small in themselves and others.

Leave a Comment