Display Unique Post Views by IP in WordPress Custom Function

Estimated reading time: 3 min


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 helpzone 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 helpzone_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 helpzone_track_post_views($post_id) {
    if (is_single()) {
        $ip_address = helpzone_get_user_ip();
        $views = get_post_meta($post_id, 'helpzone_post_views', true);
        $ip_list = get_post_meta($post_id, 'helpzone_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, 'helpzone_ip_list', $ip_list);
            update_post_meta($post_id, 'helpzone_post_views', ++$views);
        }
    }
}

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

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

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

Post Views

Step 2: Display Post Views on Your Website

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

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

echo helpzone_get_post_views(get_the_ID());

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

[helpzone_views]

🚀 Benefits of Using Post Views 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.

📊 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


Discover more from HelpZone

Subscribe to get the latest posts sent to your email.

Want to support us? Let friends in on the secret and share your favorite post!

Photo of author

Flo

Display Unique Post Views by IP in WordPress Custom Function

Published

Update

Welcome to HelpZone.blog, your go-to hub for expert insights, practical tips, and in-depth guides across technology, lifestyle, business, entertainment, and more! Our team of passionate writers and industry experts is dedicated to bringing you the latest trends, how-to tutorials, and valuable advice to enhance your daily life. Whether you're exploring WordPress tricks, gaming insights, travel hacks, or investment strategies, HelpZone is here to empower you with knowledge. Stay informed, stay inspired because learning never stops! 🚀

👍 Like us on Facebook!

Closing in 10 seconds

Leave a Reply