Set a Cookie in WordPress

cookies

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:

function set_newuser_cookie() {
	if ( !is_admin() && !isset($_COOKIE['sitename_newvisitor'])) {
		setcookie('sitename_newvisitor', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
	}
}
add_action( 'init', 'set_newuser_cookie');

You can read more about the setcookie function at PHP.net. This will set a new cookie named “sitename_newvisitor” which is set to expire in two weeks. If you changed 1 to 0, the cookie won’t expire.

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

if (isset($_COOKIE['sitename_newvisitor'])) {
     echo 'Welcome back!';
}
else {
     echo 'Hello new visitor!';
}

About Devin

I'm a WordPress developer based in Austin, Texas. Follow my projects on GitHub, or more general WordPress ramblings as @devinsays on twitter.

25 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?

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>