clear_olduser_cookie
Fires just before the user switching cookies are cleared. Fires just before user switching cookies are cleared, allowing for custom actions during this process.
add_action( 'clear_olduser_cookie', $callback, 10, 1 );
Description
Fires before user switching cookies are cleared. Developers can use this hook to perform custom actions or cleanups immediately before the authentication cookies related to user switching are removed from the browser. No parameters are passed to this hook.
Usage
add_action( 'clear_olduser_cookie', 'your_function_name', 10, 1 );
Examples
/**
* Example of how to hook into the 'clear_olduser_cookie' action.
*
* This function will be executed just before the user switching cookies are cleared.
* It can be used to perform any custom cleanup or logging.
*
* @since 1.4.0
*/
function my_custom_user_switching_cookie_cleanup() {
// Log that the cookies are being cleared.
error_log( 'User switching cookies are about to be cleared.' );
// You could potentially add custom logic here, for example, if you wanted to
// revoke a specific token associated with the old user or trigger a custom notification.
// For demonstration, we'll just log another message.
error_log( 'Performing custom cleanup actions before cookie clearing.' );
}
add_action( 'clear_olduser_cookie', 'my_custom_user_switching_cookie_cleanup', 10, 0 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/includes/user-switching.php:1398
function user_switching_clear_olduser_cookie( $clear_all = true ) {
$auth_cookie = user_switching_get_auth_cookie();
if ( ! empty( $auth_cookie ) ) {
array_pop( $auth_cookie );
}
if ( $clear_all || empty( $auth_cookie ) ) {
/**
* Fires just before the user switching cookies are cleared.
*
* @since 1.4.0
*/
do_action( 'clear_olduser_cookie' );
/** This filter is documented in user-switching.php */
if ( ! apply_filters( 'user_switching_send_auth_cookies', true ) ) {
return;
}
$expire = time() - 31536000;
setcookie( USER_SWITCHING_COOKIE, ' ', $expire, SITECOOKIEPATH, COOKIE_DOMAIN );
setcookie( USER_SWITCHING_SECURE_COOKIE, ' ', $expire, SITECOOKIEPATH, COOKIE_DOMAIN );
setcookie( USER_SWITCHING_OLDUSER_COOKIE, ' ', $expire, COOKIEPATH, COOKIE_DOMAIN );
} else {
if ( user_switching::secure_auth_cookie() ) {
$scheme = 'secure_auth';
} else {
$scheme = 'auth';
}
$old_cookie = end( $auth_cookie );
$old_user_id = wp_validate_auth_cookie( $old_cookie, $scheme );
if ( $old_user_id ) {
$parts = wp_parse_auth_cookie( $old_cookie, $scheme );
if ( false !== $parts ) {
user_switching_set_olduser_cookie( $old_user_id, true, $parts['token'] );
}
}
}
}