Set a Cookie in WordPress

Sometimes you want to detect new users on your site versus returning visitors and display different sorts of information to them. An easy way to do this is to set a cookie.

If you’re doing this via a theme, add this line to your functions.php file:

[gist id=”e83085e9c8dde9bbd448f5e6122c4a11″ file=”set-cookie-wordpress.php” lines=”1-10″]

You can read more about the setcookie function at PHP.net. This will set a new cookie named “sitename_newvisitor” with a value of 1 which is set to expire in two weeks.

To display different information to a returning user vs a new user, you could do something like this:

[gist id=”e83085e9c8dde9bbd448f5e6122c4a11″ file=”set-cookie-wordpress.php” lines=”12-20″]

Also, be aware, if your site has heavy caching, reading and setting the cookies may not work.

About Devin

I am a developer based in Austin, Texas. I run a little theme shop called DevPress and help manage a WooCommerce shop with Universal Yums. Find me on twitter @devinsays.

30 Responses

  1. Works perfect.
    Please, change setcookie line (expire section) for better readability to this:
    setcookie('sitename_newvisitor', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);

  2. Raquel

    Hi, nice article! I want to communicate domain and subdomain by cookies, that someone whos looged in domain still logged in domain, i use ur code to read the cookie generated by domain but it doesnt get any information, can u help me please?

  3. Hi, thanks for posts.. I like it, but im do not know variable cookie names?

    Example:

    <?php global $posts; $variable_names = get_post_ID();
    setcookie('cookie-'$variable_names)

    After:

    Both are not working

  4. Good cookie snippets there. I actually want to pass parameters to WordPress and then track these in the cookies. How do I pass the parameters to the set_newuser_cookie function through the permalink?

  5. Luca

    hello,

    to have the cookie not to expire, it’s the duration parameter, the third one, that should be set to 0. in this example the duration parameter is time()+1209600.

    the second parameter, 1, is the value assigned to the cookie itself.

    to have a non expiring cookie one should write something like:


    setcookie('sitename_newvisitor', 1, 0, COOKIEPATH, COOKIE_DOMAIN, false);

    1. Shecky

      @Luca: Setting the duration parameter to zero would make the cookie expire after the session closes.

      There is no way to set a cookie to not expire. Rather you must adjust the time so that it expires at a date far into the future (ie: several years)

      See RFC6265

  6. Jasmine wrote:
    February 18, 2012 at 6:36 pm
    Good cookie snippets there. I actually want to pass parameters to WordPress and then track these in the cookies. How do I pass the parameters to the set_newuser_cookie function through the permalink?

    I am trying to do the same thing. So far I have:

    function set_cookie_type($dough) {
    if (!isset($_COOKIE[‘CookieType’])) {
    setcookie(‘CookieType’, $dough, time()+60, COOKIEPATH, COOKIE_DOMAIN, false);
    }
    }
    add_action( ‘init’, ‘set_cookie_type’, 1, 1);
    do_action(‘set_cookie_type’, $dough);

    Although I can’t quite figure out exactly how to put this in my plugin to pass the variables to it.

  7. Johnny

    I have tried to put this into function.php in the theme, but it doesn’t work.

    I keepgetting this error:
    Hello new visitor!
    Warning: Cannot modify header information – headers already sent by (output started at /home/george/public_html/lotus/wp-content/themes/split/functions.php:13) in /home/george/public_html/lotus/wp-content/themes/split/functions.php on line 4

    What am i doing wrong?

  8. I’m having a similar problem to Johnny above.

    My site uses the Required+ Foundation 4 theme, WP 3.6.1.

    I’ve added the code exactly as shown above to the bottom of my child theme’s functions.php file.

    I get the following warning:
    Warning: Cannot modify header information – headers already sent by (output started at /home/pfp/public_html/ppc/wp-content/plugins/maintenance/index.php:36) in /home/pfp/public_html/ppc/wp-content/themes/pfp/functions.php on line 143

    Ultimately, I want to track which of several background images have been shown so that I can display a new (random) image each time the page is refreshed.

    Thanks,
    David R

  9. james

    For those having “cannot modify header” errors. Note that cookies need to be created BEFORE anything is sent to the web browser. So creating a plugin that just adds a cookie in the middle of the page isn’t going to work.

    You can set a cookie with an ajax call. Which, I’m playing around with…

  10. Les

    I tried this to set a cookie and when I added it to the functions.php file I got a syntax error and got locked out of my site. I was able to correct it through Filezilla but I am unsure where to add this code in the file.
    I added it at the top of the file, is there a special place to insert it?

  11. crouti

    Hi,

    Thank you very much for your post!
    I would like to display a box to get email of new users when they visit a certain category of wordpress posts, can you help me please ?
    How can i modify you snippet in order to do this ?

    Thank you!

  12. What if you’re plugin is a class and need to set a cookie after an oauth token has been sent? You can’t use the init action method at this point. Any suggestions?

Leave a Reply