How to Set, Get and Destroy Cookies in Wordpress

How can I set, get and destroy cookies in WordPress?

You can either retrieve and manipulate cookies on the server side using PHP or client side, using JavaScript.

In PHP, you set cookies using setcookie(). Note that this must be done before any output is sent to the browser which can be quite the challenge in Wordpress. You're pretty much limited to some of the early running hooks which you can set via a plugin or theme file (functions.php for example), eg

add_action('init', function() {
if (!isset($_COOKIE['my_cookie'])) {
setcookie('my_cookie', 'some default value', strtotime('+1 day'));
}
});

Retrieving cookies in PHP is much easier. Simply get them by name from the $_COOKIE super global, eg

$cookieValue = $_COOKIE['my_cookie'];

Unsetting a cookie requires setting one with an expiration date in the past, something like

setcookie('my_cookie', null, strtotime('-1 day'));

For JavaScript, I'd recommend having a look at one of the jQuery cookie plugins (seeing as jQuery is already part of Wordpress). Try http://plugins.jquery.com/project/Cookie

How to set a cookie in Wordpress

You have to set them before anything is outputted

look there: How can I set, get and destroy cookies in Wordpress?

If you are using a theme in function.php

function set_new_cookie() {
//setting your cookies there
}
add_action( 'init', 'set_new_cookie');

Your expiration date is 0 so you cookies will be deleted right away look at the php doc:

EDIT:
From php.net:

If set to 0, or omitted, the cookie will expire at the end of the
session (when the browser closes).

http://php.net/manual/en/function.setcookie.php

You have to set it like this for example :

setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */

how to get cookie php/wordpress

If you're using the following code:

setcookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200, "/", ".website.com");

This may not setup the cookie properly. One test would be to clear the cookie and test with :

setcookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200);

These parameters, Path and Domain, are optional. If they are not set correctly, even slightly, you (actually the browser) will not be able to read the Cookie properly as the HTTP URL may not match properly.

As suggested in the comments, you may want to examine this more: http://php.net/manual/en/function.setcookie.php

Something that wasn't made clear to me here and totally confused me for a while was that domain names must contain at least two dots (.), hence 'localhost' is invalid and the browser will refuse to set the cookie! instead for localhost you should use false.

To make your code work on both localhost and a proper domain, you can do this:

<?php

$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;

setcookie('cookiename', 'data', time()+60*60*24*365, '/', $domain, false);

?>

You may want to consider using this code going forward:

function set_newuser_cookie() {
if (!isset($_COOKIE['OREF2'])) {
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200, "/", $domain);
}
}

how to destroy session and cookies from same server in php?

You cant modify cookies of another domain from one domain.

so I suggest you create some kind of api like this.

on WordPress installation...

<?php

// file: wp/api/logout.php
require_once("../wp-load.php");

if(empty($_GET["email"])) {
die('no email given');
}

// get wordpress user_id by email
$email = $_GET["email"];
$user = get_user_by( 'email', $email );
$user_id = $user->ID;

// get all sessions for user with ID $user_id
$sessions = WP_Session_Tokens::get_instance($user_id);

// we have got the sessions, destroy them all!
$sessions->destroy_all();

print("user logged out.");

now if you access url of your wordpress like this(be sure to url encode email id):
https://example.com/api/logout.php?email=email%40gmail.com

it will logout user with email id: email@gmail.com

now you can do curl request from your drupal to this url.

off course this is not very secure (one can logout any user if they have email id)

How to set cookie in wordpress?

setcookie('cart_item',$carts,time() + (86400*30),'/');

Its working fine for me. / at the last is to set cookie for whole site.
Remove / to set cookie for particulat directory only.

PHP delete cookie that starts with wp_postpass_

So iterate through all cookies and check if there contain wp_postpass_ and then remove the cookie.

foreach($_COOKIE as $cookieKey => $cookieValue) {
if(strpos($cookieKey,'wp-postpass_') === 0) {
// remove the cookie
setcookie($cookieKey, null, -1);
unset($_COOKIE[$cookieKey]);
}
}


Related Topics



Leave a reply



Submit