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.