It sounds so simple, but I cannot seem to find a simple way to simply count the number of unique visitors of a single page (e.g. by using a short code on that page for a counter or a full PHP implementation).

I thought this would probably be achievable by a plugin. As an example, I tried SRS Simple hits Counter as well as Who Hit The Page - Hit Counter, but they both don't use cookies, as a new browser session shows up as another user.

Therefore, I have to implement it myself, but where to start? Can this just be done using PHP only? What is a minimum working example of this?

I want this for RSVP purposes for an event. I want to send invitations by e-mail and I want people to RSVP with a single click by referring them to that page from the e-mail invitation.

I only need a rough estimate of the number of attendees. A bonus would be if I also get the referer URL logged for each visitor so that I can make a attendent-specific RSVP URL in the invitation mailing. Another bonus would be if the user can change his mind by switching a toggle on the same page and he gets removed from the log, or the number of attendees gets decremented by one.

  • Since the first version was put on hold due to the fact I explicitly asked for a plugin, I edited it. I hope it is ok now. Can someone please re-open it?
    – Ralph
    Nov 17, 2017 at 8:56

1 Answer 1


Here's a minimum working example... (note: this is untested code.)

add_action('init', 'custom_rsvp_handler');
function custom_rsvp_handler() {
    // for singular posts only
    // (could also check for event post type here)
    if (!is_singular()) {return;}

    // set cookie expiry length (eg. 28 days)
    $cookielength = time()+(60*60*24*28);

    // set a cookie key based on post ID
    global $post; $cookiekey = 'rsvp-'.$post->ID;

    // set global to access RSVP state in shortcode
    global $rsvp;

    // check if already visited (RSVP cookie set)
    if (isset($_COOKIE[$cookiekey])) {$rsvp = $_COOKIE[$cookiekey];}

    // check for RSVP querystring (clicked email link with ?rsvp=1)
    // (or clicked attending after clicking not attending)
    if (isset($_GET['rsvp'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
        // check for email click
        if ($_GET['rsvp'] == '1') {
            $rsvp = '1'; 
            if (!isset($_COOKIE[$cookiekey])) {
                // assume this is a new click and set cookie
                setcookie($cookiekey, '1', $cookielength);
                // record the IP to the post meta
                $ips = get_post_meta($post->ID, 'rsvps');
                if ( (!is_array($ips)) || (!in_array($ip, $ips)) ) { 
                    $ips[] = $ip; update_post_meta($post->ID, 'rsvps', $ips);
                    update_post_meta($post->ID, 'rsvp-count', count($ips));
        } elseif ($_GET['rsvp'] == '0') {
            // user clicked not attending button
            $rsvp = '0'; setcookie($cookiekey, '0', $cookielength);
            // remove IP from attending list
            $ips = get_post_meta($post->ID, 'rsvps');
            if ( (is_array($ips)) || (in_array($ip, $ips)) ) { 
                    $index = array_search($ip, $ips); unset($ips[$index]);
                    update_post_meta($post->ID, 'rsvps', $ips);
                    update_post_meta($post->ID, 'rsvp-count', count($ips));

// shortcode for a non-attending button
add_shortcode('rsvp-button', 'custom_rsvp_button');
function custom_rsvp_button() {
    global $rsvp;
    if (!isset($rsvp)) {return '';}
    if ($rsvp == '1') {$action = 'I Cannot Attend'; $switch = '0';}
    if ($rsvp == '0') {$action = 'I Can Attend'; $switch = '1';}
    $button = '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
    $button .= '<input type="hidden" name="rsvp" value="'.$switch.'">';
    $button .= '<input type="submit" value="'.$action.'">';
    $button .= '</form>';
    return $button;

add_shortcode('rsvp-count', 'custom_rsvp_count');
function custom_rsvp_count() {
    global $rsvp, $post;
    if (!isset($rsvp)) {return;}
    $attendees = get_post_meta($post->ID, 'rsvp-count');
    if ($attendees) {return $attendees." Attending";}
    return '';

So the email link RSVP URL would be like: http://example.com/event-post/?rvsp=1

This will save a list of IPs to the event post's meta, and set a cookie for the visitor as attending which will (roughly) prevent duplicate records.

Then you would add the shortcode [rvsp-button] into your event post content so that page visitors could click "I Cannot Attend" after visiting the page. Or "I Can Attend" if they have already clicked that they cannot. (Visitors that did not arrive through the email link will not see the button.)

UPDATE: added post meta and shortcode [rsvp-count] for outputting RSVP attending count.

  • sweet let me know how it goes... realized you may need the attending count so I added a shortcode for that also. :-)
    – majick
    Nov 28, 2017 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.