Adding NTFY message notifications to a WordPress blog: Notify me every time a real user visits a post.

Spread the love

Add code

Click the theme editor

Click on function.php on the right

Place the code at the very bottom of the file.

Content of the notice

New Article View
Someone is reading your article!

Title: Daily life of a digital nomad…
Domain: xxx.xxx.com
URL: https://xxx.xxx.com/your-article-slug/
Device: iPhone (Mobile)
OS: iOS 18.1
Browser: Chrome 131.0.6778.85
Time: 2025-11-23 23:15:27 (Beijing/Shanghai)

Code snippet

This is the optimized code.

The previous code sent messages via web crawlers, Linux control panels, and ad agency detection systems.

I only want to receive notifications for genuine user visits, not from bots, web crawlers, or advertisers.

I’ve tested the code myself; its effectiveness will require further testing over the next few days.

// ================ Real Human Visitor Notification - English Version ================

function real_visitor_notify_en() {
    if (is_user_logged_in() || is_admin() || is_feed() || is_preview() || is_robots()) return;
    if (!is_singular()) return;
    if (empty($_GET['rh'])) return;

    $title     = get_the_title();
    $host      = $_SERVER['HTTP_HOST'];
    $raw_url   = 'https://' . $host . $_SERVER['REQUEST_URI'];
    $clean_url = remove_query_arg('rh', $raw_url);
    $ua        = $_SERVER['HTTP_USER_AGENT'] ?? '';

    // OS + version
    $os_full = 'Unknown OS';
    if (preg_match('/Windows NT (\d+\.\d+)/i', $ua, $m)) {
        $v = $m[1];
        $os_full = ['10.0'=>'Windows 10/11','6.3'=>'Windows 8.1','6.2'=>'Windows 8','6.1'=>'Windows 7'][$v] ?? "Windows $v";
    } elseif (preg_match('/Mac OS X (\d+[\._\d]+)/', $ua, $m)) {
        $os_full = 'macOS ' . str_replace('_', '.', $m[1]);
    } elseif (preg_match('/Android ([\d\.]+)/i', $ua, $m)) {
        $os_full = 'Android ' . $m[1];
    } elseif (preg_match('/iPhone OS ([\d_]+)/i', $ua, $m)) {
        $os_full = 'iOS ' . str_replace('_', '.', $m[1]);
    } elseif (preg_match('/Linux/i', $ua)) {
        $os_full = 'Linux';
    }

    // Browser + version
    $browser_full = 'Unknown Browser';
    if (preg_match('/Edg\/([\d\.]+)/i', $ua, $m))          $browser_full = 'Edge ' . $m[1];
    elseif (preg_match('/Chrome\/([\d\.]+)/i', $ua, $m))   $browser_full = 'Chrome ' . $m[1];
    elseif (preg_match('/Firefox\/([\d\.]+)/i', $ua, $m))  $browser_full = 'Firefox ' . $m[1];
    elseif (preg_match('/Version\/([\d\.]+).*Safari/i', $ua, $m)) $browser_full = 'Safari ' . $m[1];

    // Device
    $is_mobile = preg_match('/Mobile|Android|iPhone|iPad|iPod/', $ua);
    if (strpos($ua, 'iPhone') !== false)      $device = 'iPhone';
    elseif (strpos($ua, 'iPad') !== false)    $device = 'iPad';
    elseif (strpos($ua, 'Macintosh') !== false) $device = 'MacBook';
    elseif (preg_match('/Android[^;]*; (.*?)Build/i', $ua, $m)) $device = trim($m[1]) ?: 'Android Device';
    else $device = 'PC';

    // Beijing/Shanghai time (forced)
    $time = wp_date('Y-m-d H:i:s', null, new DateTimeZone('Asia/Shanghai'));

    $message = "Someone is reading your article!\n\n"
             . "Title: {$title}\n"
             . "Domain: {$host}\n"
             . "URL: {$clean_url}\n"
             . "Device: {$device}" . ($is_mobile ? ' (Mobile)' : ' (Desktop)') . "\n"
             . "OS: {$os_full}\n"
             . "Browser: {$browser_full}\n"
             . "Time: {$time} (Beijing/Shanghai)";

    wp_remote_post('https://ntfy.dsx2016.com/nomad', [
        'blocking' => false,
        'timeout'  => 3,
        'body'     => $message,
        'headers'  => [
            'Title' => 'New Article View',
            'Tags'  => 'book,eyes,computer',
            'Click' => $clean_url,
        ],
    ]);
}
add_action('wp', 'real_visitor_notify_en');

// Footer JS trigger
add_action('wp_footer', function() {
    static $done = false;
    if ($done) return;
    $done = true;
    echo '<script>if(location.search.indexOf("rh=")===-1){let u=new URL(location.href);u.searchParams.set("rh","1");navigator.sendBeacon?.(u)||fetch(u,{method:"HEAD",keepalive:true});}</script>';
}, PHP_INT_MAX);

Tips

The previous code was optimized by chatgpt dozens of times, but they still messed with me.

This time, the code provided by grok 4.1 also underwent more than ten revisions.

If it still doesn’t work, I’ll optimize it myself later.

Leave a Reply

Your email address will not be published. Required fields are marked *