Firefox + setcookie() + WordPress = wrong cookie data

Today I had to deal with an issue for my customer where the cookie data was not as expected.

I have a template on the site that is used for vanity URLs. Each new sign up gets one. It is used to display a page and the author is set to be the new sign up. This page links to other non-Wordpress pages that deal with referrals and what not. In order to make sure the right person gets credit for the referral, I set a cookie with the author info. This is done with the setcookie() PHP function in the template file.

What was happening was that some people were visiting a vanity URL and then moving on to the sign up page only to see the wrong person as the referrer. When I looked into this, being that I primarily use Google Chrome, I noticed that it was working as intended. I checked to see what browser was being used when this issue was able to be recreated and it was Firefox. Just for kicks, I checked in IE and to my surprise, it worked as intended in IE too! This has to be the only time that IE works right and Firefox doesn’t.

It really perplexed me that the browser used would generate different PHP results as PHP is parsed server side and should have nothing to do with the browser at all.

After some testing, I was able to see that the cookie was being set correctly and then immediately set to contain the wrong contents. What I found odd about this was that it was very consistant. The same author would always result in the same wrong author data populating the cookie.

After taking this issue to Google, I found this article, here which led me to here. What this article points out is that WordPress will add to the head a link to the next page and previous page. I didn’t look into how these are determined, but having that link there led to the problem. Firefox automatically loads the next page. This causes the server to parse the template PHP and thus reset the cookie to the wrong author.

By adding

remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

to the theme’s function.php file, this issue was solved.

I can’t wait to see what other odd WordPress issues will turn up…..