Created
August 9, 2015 10:54
-
-
Save Schwankenson/caf746e2ffc71b1db243 to your computer and use it in GitHub Desktop.
Set wordpress cookie as root cookie for your domain and all subdomains
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
// 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