简体   繁体   中英

How do I set a cookie in Wordpress?

I'm having a problem with a cookie not serving. We're adding this cookie as a workaround to stop the blocking of 3rd party cookies in both Safari and Firefox browsers.

The below code is what I have included in our Wordpress child theme functions.php file (currently in place on our production server.

Could someone please review and advise if you feel any changes are required. Thanking you in advance.

function set_my_cookie() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$user_agent = strtolower($user_agent);
$user_agent = ' ' . $user_agent;
$browser    = '';

if (strpos($user_agent, 'opera') || strpos($user_agent, 'opr/')) $browser = 'Opera';
else if (strpos($user_agent, 'edge')) $browser = 'Edge';
else if (strpos($user_agent, 'chrome')) $browser = 'Chrome';
else if (strpos($user_agent, 'safari')) $browser = 'Safari';
else if (strpos($user_agent, 'firefox')) $browser = 'Firefox';
else if (strpos($user_agent, 'msie') || strpos($user_agent, 'trident/7')) $browser = 'Internet Explorer';

if ($browser == 'Safari') {
$urlparts = parse_url(home_url());
$domain   = '.' . $urlparts['host'];
$time   = strtotime("+1 year");

if (!isset($_COOKIE['kppid'])) {
$kppid = uniqid('sf', true);
$kppid = str_ireplace('.', '', $kppid);

setcookie('x', $x $time, '', $domain);
} else {
$kppid = $_COOKIE['kppid'];
setcookie('x', $x, $time, '', $domain);
}
}
}

You have to create a init hook for cookie, see

add_action( 'init', 'my_setcookie' );
function my_setcookie() {
  setcookie( $name, $value, 24 * DAYS_IN_SECONDS, COOKIE_PATH, COOKIE_DOMAIN );
}

Then the cookies can be get by using setcookie function.

echo "My cookie: " . $_COOKIE[$name];

By default, WordPress uses cookies to manage logged-in user sessions and authentication. It also uses cookies to remember a user's name and email address if they fill out a comment form.

However, many WordPress plugins on your website may also set their own cookies. For example, OptinMonster allows you to show different email optin forms to new vs returning visitors, and it does that by using cookies.

If you are using third party services on your website like Google Analytics or Google AdSense, then they may also set cookies on your website.

So, you can also add cookies to your own WordPress site in same manner.

you will need to add code to your theme's functions.php file or a site-specific plugin. If you haven't done this before, then please take a look at how to copy and paste code snippets in WordPress .

First we will use the setcookie() function in PHP. This function accepts the following parameters.

  • Cookie name
  • Cookie value
  • Expire (Optional: sets a time period after which cookie expires)
  • Path (Optional, by default it will use the site's root)
  • Domain (Optional, by default uses your website's domain)
  • Secure (Optional, If true then only transfers cookie data via HTTPS)
  • httponly (Optional, when set true the cookie is only accessible via HTTP and cannot be used by scripts)

Now let's add a code snippet to your WordPress site. This code stores the exact timestamp when a user visited your website in a cookie.

function wpb_cookies_tutorial1() { 
$visit_time = date('F j, Y  g:i a');
if(!isset($_COOKIE[$wpb_visit_time])) {
// set a cookie for 1 year
setcookie('wpb_visit_time', $current_time, time()+31556926);
}
} 

You can now visit your website and then check your browser cookies. You will find a cookie with the name wpb_visit_time .

Feel free to modify the code to make it more useful for your website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM