42 Responses to “Setting Cookies In WordPress – Trap For Beginners”

This page contains comments from the Setting Cookies In WordPress – Trap For Beginners article.

  1. I’m not sure if this is any good to you but have you heard of Openx? It is an open source ad management program – I have been using it to geotarget adsense but it also has functions such as only showing ads after so many visits etc.

    Pretty easy to use and may be worth a look. Just google openx.

  2. Online savings account says:

    That’s a very good open source tool. I have used it to monitor unique visits from SE. Will paste my code here.

    LINK REMOVED: because of failure to use KeywordLuv syntax (name@keywords)

  3. John@St. Louis Wedding Photography says:

    Thanks for the follow up with the fix!

  4. I’m using Openx too on some of my sites. It has proven itself great, you should try it!

    • I’m surprised that wordpress doesn’t have a convenient API already set for cookies. I’m developing a plugin that will require a cookie, so found your blog this way. The closest thing I found in the true WordPress API was wp_setcookie(), which is for storing auth data only. I guess I’ll just do it the dirty way!

  5. I haven’t tried OpenX yet. I’m using the “Who Sees Ads” plugin to limit AdSense being displayed to search visitors, although I could probably hard code the parameters into place. I’m lazy when it comes to coding, so I’ll probably leave it as is.

    On using cookies to continue with search visitors… I’m not too keen on doing that. If someome moves from post to post, they’re obviously finding something they’re looking for and that completely defeats getting clicks. I suspect that the CTR would nearly drop to half of what it was before. Now, for non-AdSense pages, it’s probably a good idea — for any service that doesn’t do any kind of smart pricing.

    • Hi RT,

      On using cookies to continue with search visitors… I’m not too keen on doing that. If someome moves from post to post, they’re obviously finding something they’re looking for and that completely defeats getting clicks. I suspect that the CTR would nearly drop to half of what it was before

      I suspect you’re right and it’s less likely they’ll click an add, but they are obviously looking for something and IF the link they clicked doesn’t give them the answer, there’s still a decent chance they’ll click on an ad before exiting.

      It would reduce the CTR somewhat, but it would still be higher than normal users and I don’t believe CTR affects smart pricing. I know that’s what Court said, but I follow the Grizzly train of thought (that it’s really about targeted traffic for the advertisers and CTR is just bi-product of that).

      So the real question is does Google count them as targeted traffic, because it remembers what they searched for? Or does it only recognise that they came from another page on your site?

  6. That’s a very good open source tool. I have used it to monitor unique visits from SE. Will paste my code here. Apologies for using the wrong format Stephen.

  7. I use “Who Sees Ads” plugin as well and it certainly helps. I am curious as to what kind of rules do you guys follow in the plugin. I have chosen to shows ads to search engine visitors or if the post is 7 days old. I see various options in there but curious if someone has fine tuned those further and your reasoning behind it.

    • Hi K,

      I don’t use Who Sees Ads, but I just show Adsense to the search engine visitors, as a) they’re the ones who are really likely to click ads and b) other traffic can increase your chance of getting smart priced if they do click.

      In some cases posts older than 7 days will be mostly search engine traffic anyway, but occasionally I get a StumbleUpon run on old posts…

  8. OpenX wow, thanks for the info:)

    Never thought about that.

    D

  9. Hi All who wrote about OpenX,

    I’ve heard of OpenX but never tried it. It sounds like it’s great – but can it display only for search engine visitors? You really don’t want to show Adsense to your StumbleUpon traffic…

  10. Roger Hamilton says:

    Thanks for the useful information . I would love to try it . Hope to see more articles like this in the future. Thanks a lot.

  11. Jeff@Forth Mill Real Estate says:

    Thanks a lot for the excellent post! It is very helpful to me and I found exactly what I want in your site. keep it up and I am looking forward to hear more.

  12. Ty, Cool post, Forgot about basic functions to help with cookies

  13. Jules@Get A Copy Of My Credit Report says:

    Hi Stephen,

    I really appreciate the follow up post to correct this bug. I’ve finally got it working and really appreciate the thoroughness of your explanations:)

  14. Gerry@Money Making Consultants says:

    Nice post. . Openx sound new to me and when I read about it it makes me so interested. I will surely try this one.

  15. Thanks Stephen, that’s useful stuff

    Hope it’s OK to digress slightly? Talking of geo-targetting, is there a way to geo-target affiliate links? For example with Amazon links, so that UK visitors see the link to Amazon.co.uk, USA visitors see the link to .com

    Cheers, Jon

    • Hi Jon,

      Well, I don’t use affiliate links much and it will depend on the individual affiliate, but it should be possible to do something. In theory, you could check where someone’s from, then display a certain link. But doing it would be a little complicated.

      I’ll think about this for a future post, but no promises on when I may get to it as I’m flat out right now..

  16. The domain parameter is there so you can restrict the accessibility of the cookie to just the domain you specify. If you put a dot before the domain, that also makes it available to all the subdomains. I would advise to ALWAYS use it this parameters, or else other sites could potentially pick up the cookies that your site dropped.

  17. Lesley Brown says:

    Ah I think this is why my wasnt working, doh!

  18. chris says:

    You could also get the server info via PHP instead of using the WordPress setting so it would work on non-wp pages as well. Otherwise you’d need to include the blog header.

  19. Austin says:

    In general, it’s probably better to go with the WordPress constants already defined for the cookie path and domain, as the processing work figuring out their values has already been done, and that way you’ll match the existing WordPress cookies:

    setcookie("sevisitor", 1, time()+3600, COOKIEPATH, COOKIE_DOMAIN);

    • Hi Austin,

      Great tip! I didn’t know these constants existed. COOKIE_DOMAIN isn’t mentioned on the WordPress Codex at all, and COOKIEPATH only has a few obscure mentions. I guess the WordPress Codex needs a list of constants that are available. Thanks for pointing this out.

  20. Eda says:

    thanks a lot, it works just great

  21. Jenny says:

    Good idea to have a cookie here…I think this code should be placed in a loop such that it is activated until the process is done…Thanks for the post.

  22. Tom@Recording Studios says:

    I just tried to understand(I get the .your domain.com idea, and I think I understand The 2nd part too, but I decided to bookmark the page so I can come back while I’m actually setting up the cookie for my blog.
    I’m still in the stage where I’m crossing my fingers and hoping I don’t screw anything up. (i will be holding my breath while doing it). thanks for sharing the help.
    Tom

  23. You should make a complete wordpress template with your plugings so people could just install all of them with your adsense plugin in one shot. I am sure it would be very popular.

  24. That’s a very good open source tool. I have used it to monitor unique visits from SE. Will paste my code here. Apologies for using the wrong format Stephen.

  25. Curt says:

    hey man this is simply new to me that cookie can be set first time. ‘set cookie command’s domain parameter’ i did not even know that this command exist. thanks man for this great piece of information. i will try to gather more knowledge about this.

  26. Rob says:

    Thanks for this tip. I have an area of my home page dedicated to an animated carousel of video selections. Some users have said they’d like to hide it once and not see it again, even after leaving and returning, until they choose to unhide it. Most visitors are anonymous visitors.

    I was wondering if you or any of your commenters has insight into how to modify this cookie setting code so that it will remember whether a person has, at any time, clicked a “show/hide” link and “remember” their preference on the client.

    Thanks for any help you can give me.

  27. Steven says:

    humm…. it’s not owkring for me. I’m developing a plugin and I keep getting the error message: Warning: Cannot modify header information – headers already sent by (…).

    Any ideas how I can get around this? wp_setcoockie() doesn’t work either.

    • Hi Steven,

      There could be other reasons, but the most common reason for that error is that there is a space before the opening ) in one of your files. That said, there can often be conflicts with other plugins… That’s probably not much help, but it’s somewhere to start.

  28. jane says:

    hi,
    I am setting cookie as setcookie(‘Test’, “displayName”, time() + 3600, get_bloginfo(‘url’)); in header.php in wordpress,and i am displaying trying to retrieve it in comments.php,but i am not able to retrive the cookie value in comments.php.
    can any one help me .

  29. k says:

    omg! spent an hour wondering why my cookie wont be retrieved, yet it would outside wordpress

    the / & domain were all i needed

  30. Ian McKellar says:

    FWIW, I did this:

    $url = parse_url(get_bloginfo(‘url’));
    setcookie(‘name’, ‘value’, $expires, $url['path'], $url['host']);

    that’ll get you the actual host and path of the blog.

  31. Devin Price says:

    Try this:

    // Set cookie for new user
    function set_newuser_cookie() {
    if (!isset($_COOKIE['newvisitor'])) {
    setcookie(‘newvisitor’, 1, time()+86400, COOKIEPATH, COOKIE_DOMAIN, false);
    }
    }
    add_action( ‘init’, ‘set_newuser_cookie’);

  32. Dietmar says:

    Hi, there is a neat method to avoid using the str_replace command: There are two definitions inside WordPress, that can do the job for you:
    1. COOKIEPATH: Stores the right path to your cookies
    2. COOKIE_DOMAIN: Since WordPress 3.0 this definition isn’t set intially within the wp-config.php, so you will need to set this to what you want.

    Cheers
    Dietmar

  33. Jurre says:

    Instead of messinga round with str_replace() you can also just use the PHP function parse_url() to get the domain from the URL.

  34. Hi!

    I doubt using the str_replace function, in this case, would cause any notable difference in speed. Not even worth taking into account. We’re talking about microseconds: millionths of a second.

  35. Kaiser says:

    You should use the core constant SITECOOKIEPATH instead.

Leave a Reply

Anti-Spam Quiz:


Stephen Cronin

is Manager of Online Service Delivery at a Queensland Government department & has been a freelance WordPress developer/consultant since 2007
*Content on this site is my own and is not related to my employer

Hire me - I'm expensive, but I'm very good!

WordPress Plugins

Want a Custom WordPress plugin? See my Services page.

Greasemonkey Scripts

Visit my home page at Userscripts.org.