Skip to content

Instantly share code, notes, and snippets.

@carl-alberto
Last active April 23, 2020 21:09
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 carl-alberto/224bd25a106eaf9d0a54f5438950a249 to your computer and use it in GitHub Desktop.
Save carl-alberto/224bd25a106eaf9d0a54f5438950a249 to your computer and use it in GitHub Desktop.
Cookie name override
<?php
// Replace badcookiename with the cookie that you are trying to override
// Add in a custom plugin or theme's functions.php
add_action(
'init',
function() {
if ( isset( $COOKIE['wp-goodcookiename'] ) ) {
$COOKIE['badcookiename'] = $COOKIE['wp-goodcookiename'];
}
},
5
);
add_action(
'wp_loaded',
function() {
foreach ( headerslist() as $header ) {
if ( stripos( $header, 'Set-Cookie: badcookiename' ) !== false ) {
$cookieValue = substr( $header, strlen( 'Set-Cookie: ' ) );
$cookieParts = [];
$cookieValue = urldecode( $cookieValue );
foreach ( explode(
'; ',
$cookieValue
) as $k => $v ) {
preg_match(
'/^(.?)=(.?)$/i',
trim( $v ),
$matches
);
$cookieParts[ trim( $matches1 ) ] = urldecode( $matches2 );
} setcookie(
'wp-goodcookiename',
$cookieParts['badcookiename'],
strtotime( $cookieParts['expires'] ),
$cookieParts['path'],
$cookieParts['domain']
);
}
} },
25
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment