Setting Cookies In WordPress – Trap For Beginners
September 26th, 2008 by Stephen Cronin (Please wait) [Shortlink]I recently wrote about displaying ads only to search visitors in WordPress. A key part of the technique described is to set a cookie, identifying the visitor as having come from a search engine. It should have been simple, but my PHP setcookie command didn’t appear to work.
I was using the setcookie command in the same way I’d done countless times before. I’d already taken into account that the cookie wouldn’t be available on the first page view. I couldn’t see anything wrong with the command I was using (in functions.php in the WordPress theme I was creating):
setcookie("sevisitor", 1, time()+3600);
This should have set a cookie with a name of sevisitor and a value of 1, that expires after one hour. However, no cookie was created.
After scouring the Internet in vain, I found a hint somewhere in the WordPress Support Forums (although I can’t find the page again now!). It suggested that the setcookie command’s domain parameter needed to be set.
I’d never used this parameter before and although I’ve seen explanations of what it does, I still can’t find a clear explanation of why you’d want to use it. Most descriptions of it mention that it’s optional, but don’t mention under what circumstances it would be necessary. Any PHP experts out there, please feel free to put me straight in the comments!
Anyway, after some experimentation, I got it working, by using the following command:
setcookie("sevisitor", 1, time()+3600, "/", ".scratch99.com");
Problem solved! As you can see I’ve done the following:
- added the path parameter, setting it to “/”, which allows the cookie within the entire domain.
- added the domain parameter, setting it to my top level domain (.scratch99.com). Note the preceding . apparently makes it compatible with more browsers.
Obviously, if you want to set a cookie in WordPress, you’ll have to replace .scratch99.com with the domain you are using.
Alternatively, you could let WordPress work out the domain for you, by using the following:
setcookie("sevisitor", 1, time()+3600, "/", str_replace('http://www','',get_bloginfo('url')) );
This uses the get_bloginfo('url')) command to get the site’s URL from WordPress, then strips the preceding http://www from it, leaving only the domain. Note, if you don’t use the www on your site, you’ll need to change:
str_replace('http://www'
to
str_replace('http://'
However, it’s really not worth doing things this way – your site will be slightly faster if you hard code your domain in.
The only time you’d want to use the above would be if you were putting this into a WordPress Theme that you were creating to be distributed. In that case, you’d probably want to make it a little cleverer, so that it works whether or not the www is present. But I’ll leave that to you to work out!
Anyway, the lesson I learned: If you want to set a cookie in WordPress, you must set the path and domain parameters.

