Skip to content
All gists
Back to GitHub
Sign in
Sign up
Sign in
Sign up
{{ message }}
Instantly share code, notes, and snippets.
devinsays
/
set-cookie-wordpress.php
Last active
Feb 7, 2021
Star
2
Fork
1
Star
Code
Revisions
2
Stars
2
Forks
1
Embed
What would you like to do?
Embed
Embed this gist in your website.
Share
Copy sharable link for this gist.
Clone via HTTPS
Clone with Git or checkout with SVN using the repository’s web address.
Learn more about clone URLs
Download ZIP
How to set a cookie using PHP in WordPress
Raw
set-cookie-wordpress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
<?php
/**
* Sets a cookie sitename_new_visitor if it doesn't exist already.
*/
function
example_set_new_user_cookie
() {
if
( !
is_admin
() && !
isset
(
$
_COOKIE
[
'sitename_new_visitor'
] ) ) {
setcookie
(
'sitename_new_visitor'
,
1
,
time
() +
3600
*
24
*
100
,
COOKIEPATH
,
COOKIE_DOMAIN
,
false
);
}
}
add_action
(
'init'
,
'example_set_new_user_cookie'
);
/**
* Displays a different message based on whether the sitename_new_visitor cookie is set or not
*/
if
(
isset
(
$
_COOKIE
[
'sitename_new_visitor'
] ) ) {
echo
'Welcome back!'
;
}
else
{
echo
'Hello new visitor!'
;
}
Sign up for free
to join this conversation on GitHub
. Already have an account?
Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.