WordPress header called twice / loading other posts in background

Jul
30
2013

So recently I was doing some stuff where I was putting a random value from an array into a session variable for use after the next page refresh. This had to happen on every page, for some reason the value kept changing and it took me a while to realize it was because the code was being called twice somehow. But I only had it in one place and it wasn’t repeated anywhere. I was perplexed until I noticed in the net tab of firebug that the theme was loading another post shortly after rendering the current page. I checked the source code and there it was

wp_head() was embedding 2 links to the adjacent posts, effectively pre caching them in the browser. Definitely a nifty feature, but it was messing with my custom stuff. Now that I knew what the precise problem was, a quick Google turned up the solution and I figured I’d share it so you don’t pull out as much hair as I did if something similar happens to you.

Throw the following somewhere in your theme (functions.php is your best bet), this will stop WordPress from generating those two link tags in wp_head();

If you want to retain the caching functionality, you could also add your own hooks that spit out the link tags, but pass a url parameter so you know when a page is being called like this.