Skip to content

Instantly share code, notes, and snippets.

@Schwankenson
Created August 9, 2015 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schwankenson/caf746e2ffc71b1db243 to your computer and use it in GitHub Desktop.
Save Schwankenson/caf746e2ffc71b1db243 to your computer and use it in GitHub Desktop.
Set wordpress cookie as root cookie for your domain and all subdomains
// If you include this in your functions.php, the wordpress cookie is set for the current domain
// and all subdomains
class RootCookie {
private static $cookie_name;
static public function init() {
self::$cookie_name = "wordpress_logged_in_" . md5(get_site_url());
if ( is_user_logged_in() ) {
self::setRootCookie();
}
else {
self::removeRootCookie();
}
}
static function getDomainWithoutSubdomain() {
$domain = parse_url(get_site_url(), PHP_URL_HOST);
$domain = explode('.', $domain);
$domain = array_reverse($domain);
$domain = "$domain[1].$domain[0]";
return $domain;
}
static function setRootCookie() {
$domain = self::getDomainWithoutSubdomain();
setcookie(self::$cookie_name, $_COOKIE[self::$cookie_name], 0, "/", $domain, false, true);
}
static function removeRootCookie() {
$domain = self::getDomainWithoutSubdomain();
setcookie(self::$cookie_name, "", time()-1000, "/", $domain, false, true);
}
}
RootCookie::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment