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.
you know your code overflow hidden after cookiepath?
Thanks. Looks like code syntax highlighting wasn’t set. I updated it.
A simple php code with great use in web developing.
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);
Thanks, Devin,
I was thinking about cookies & WordPress right this morning when still waking up. You made my day. Ciao!
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?
and how can I pass parameters to the set_newuser_cookie function
thanks
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
thank you for this great snippet!
it was great for showing a banner to visitors just one time!!
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?
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);
@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
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.
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?
Just couldn’t leave this unnatended. You gotta wrap that functionality in an if(!is_admin()) { … }
Thanks Ben. Code has been updated.
Always you and your posts!
Thanks so much, I was going mad with this damned cookies. Bless
COOKIEPATH
andCOOKIE_DOMAIN
! :DThanks again Davin,
Sebastian
Hi this is nice article. but i want to add cookies while i load my body class any suggestion.
it works perfectly, thanks for sharing!
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
It might be better to use javascript and local storage for something like that.
Just the information that I needed about cookies thanks.
I’ve probably referred to this post a 1/2 dozen times since it went up. Just want to say thanks for publishing this.
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…
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?
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!
i just use this for new visitor to show a promotion popup banner, its works great with me and save huge time, thanks bro…
You can add HTTPS support with ‘is_ssl()’ at the end:
setcookie( ‘sitename_newvisitor’, 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
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?
Setting up cookies is sometimes hard to do. But at least i learned good things here!
Thanks
Best Regards.