Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On a CPT page I am checking to see if the user is logged in; if they are, I'm checking to see if they have a meta entry which is 'session-' + the current page's stub. If they do, I increment the value by one.

I thought this was all working fine, but I've just noticed that it's actually incrementing two separate user_meta fields. At the moment I have three CPT pages (let's call them Burt, Ernie & Ralph); if I refresh the page for Burt, it increments my user_meta field (session-burt) by 1, which is great; however it also increments session-ralph by 1.

I don't have the Ralph page open, and I'm echoing out everything I can think of to try and trace down the erroneous line of code, but I can't see why it's doing it. It is incredibly annoying.

Here's my code. Hopefully I'm just blind to something really obvious and it's not a WP bug.

$stub = $post->post_name;

$sessionKey = 'session-' . $stub;
$viewedKey = $stub . '-lastviewed';

$sessionViewsArray = get_user_meta($current_user->ID, $sessionKey);
$sessionViews = (int)$sessionViewsArray[0];

echo '<pre>';
print_r($sessionViewsArray);
echo '</pre>';

echo '<h1>I think there are ' . $sessionViews . ' views</h1>';

$lastViewedArray = get_user_meta($current_user->ID, $viewedKey);
$lastViewedTime = $lastViewedArray[0];

if ($lastViewedTime) {
    $lastViewed = date('l jS F Y, g:ia', $lastViewedTime);
} else {
    $lastViewed = 'First time - good luck!';
}

$newSessionViews = $sessionViews + 1;

update_user_meta($current_user->ID, $sessionKey, $newSessionViews);
update_user_meta($current_user->ID, $viewedKey, time());

echo '<h1>I think I just updated ' . $sessionKey . '</h1>';

///
//  Now let's update the page views
///

$pageViewsArray = get_post_meta($post->ID, 'totalviews');
$pageViews = (int)$pageViewsArray[0];

$pageViews += 1;

update_post_meta($post->ID, 'totalviews', $pageViews);

It's worth noting that it only seems to be the one session-ralph entry that is affected; the ralph-lastviewed entry doesn't get updated if I refresh the Burt page.

Any help would be greatly appreciated!

UPDATE:

As per the awesome answer from @dalbaeb below, my solution was to use the following hook:

remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

...which removed the <link rel='next'... and <link rel='prev'... tags from my header and immediately solved the issue.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Are you by chance using Firefox to test your page? If so its prefetching function combined with WordPress rel='next' could be the culprit. Check your page source for those rel tags, and see if Firebug registers an extra GET request to the page specified in the href attribute of the rel tag.

There is a Firefox extension Fasterfox, and it will prefetch everything. Disabling adjacent_posts_rel_link_wp_head will not help.
You should exit early in your code in those cases:

if ( isset ( $_SERVER["HTTP_X_MOZ"] ) and 'prefetch' === $_SERVER["HTTP_X_MOZ"] )
    return;
share|improve this answer
2  
Check for isset ( $_SERVER["HTTP_X_MOZ"] ). –  toscho Apr 15 '13 at 18:38
    
@dalbaeb That is exactly right - I was using Firefox, and using the hook remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); to remove the rel links solved the issue. I've updated my question with this. –  indextwo Apr 16 '13 at 10:13
    
@indextwo See my update to this answer. Your solution is not complete enough. –  toscho Apr 16 '13 at 10:18
    
@toscho I appreciate that there may be a Firefox extension, however escaping the entire routine in these cases would actually make the displayed data seem more erroneous, as no figures would ever be incremented. I've tested it and it works for me, and the client has tested it and it works for her. –  indextwo Apr 16 '13 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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