set_olduser_cookie
Fires immediately before the User Switching old user cookie is set. Fires just before the old user cookie is set, allowing modification of cookie details before it's written.
add_action( 'set_olduser_cookie', $callback, 10, 5 );
Description
Fires just before the User Switching plugin sets the cookie for the original user when switching back. Developers can modify the cookie value, expiration, user ID, authentication scheme, or session token. This hook is ideal for custom cookie management or logging user switch actions.
Usage
add_action( 'set_olduser_cookie', 'your_function_name', 10, 5 );
Parameters
-
$olduser_cookie(string) - The old user cookie value.
-
$expiration(int) - The time when the logged-in authentication cookie expires as a UNIX timestamp.
-
$old_user_id(int) - User ID.
-
$scheme(string) - Authentication scheme. Values include 'auth' or 'secure_auth'.
-
$token(string) - User's session token to use for this cookie.
Examples
// Hook into the 'set_olduser_cookie' action to perform an action before the old user cookie is set.
add_action( 'set_olduser_cookie', 'my_custom_olduser_cookie_handling', 10, 5 );
/**
* Example function to handle the 'set_olduser_cookie' action.
* This function demonstrates logging information about the old user cookie being set.
*
* @param string $olduser_cookie The old user cookie value.
* @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp.
* @param int $old_user_id User ID of the previously logged-in user.
* @param string $scheme Authentication scheme ('auth' or 'secure_auth').
* @param string $token User's session token.
*/
function my_custom_olduser_cookie_handling( $olduser_cookie, $expiration, $old_user_id, $scheme, $token ) {
// You can perform various actions here, for example:
// 1. Log the cookie information for debugging or auditing purposes.
error_log( sprintf(
'User Switching: Setting old user cookie. User ID: %d, Scheme: %s, Expiration: %s, Token: %s, Cookie Value: %s',
$old_user_id,
$scheme,
date( 'Y-m-d H:i:s', $expiration ),
$token,
$olduser_cookie // Be cautious logging sensitive cookie values in production.
) );
// 2. Potentially modify or extend the cookie's expiration if needed, though the hook is an action,
// direct modification of parameters isn't typical for actions. If modification is the goal,
// consider if a filter hook would be more appropriate or if setting a new cookie is intended.
// For this example, we'll assume we just want to react to the cookie being set.
// 3. Add custom logic based on the user ID, scheme, or other parameters.
if ( user_can( $old_user_id, 'administrator' ) ) {
error_log( sprintf( 'User Switching: Old user cookie set for an administrator (User ID: %d).', $old_user_id ) );
}
// Since this is an action hook, there's no explicit return value required for the hook itself.
// If your function were a filter, you would return the modified value.
}
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:1362
function user_switching_set_olduser_cookie( $old_user_id, $pop = false, $token = '' ) {
$secure_auth_cookie = user_switching::secure_auth_cookie();
$secure_olduser_cookie = user_switching::secure_olduser_cookie();
$expiration = time() + 172800; // 48 hours
$auth_cookie = user_switching_get_auth_cookie();
$olduser_cookie = wp_generate_auth_cookie( $old_user_id, $expiration, 'logged_in', $token );
if ( $secure_auth_cookie ) {
$auth_cookie_name = USER_SWITCHING_SECURE_COOKIE;
$scheme = 'secure_auth';
} else {
$auth_cookie_name = USER_SWITCHING_COOKIE;
$scheme = 'auth';
}
if ( $pop ) {
array_pop( $auth_cookie );
} else {
array_push( $auth_cookie, wp_generate_auth_cookie( $old_user_id, $expiration, $scheme, $token ) );
}
$auth_cookie = json_encode( $auth_cookie );
if ( false === $auth_cookie ) {
return;
}
/**
* Fires immediately before the User Switching authentication cookie is set.
*
* @since 1.4.0
*
* @param string $auth_cookie JSON-encoded array of authentication cookie values.
* @param int $expiration The time when the authentication cookie expires as a UNIX timestamp.
* @param int $old_user_id User ID.
* @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'.
* @param string $token User's session token to use for the latest cookie.
*/
do_action( 'set_user_switching_cookie', $auth_cookie, $expiration, $old_user_id, $scheme, $token );
$scheme = 'logged_in';
/**
* Fires immediately before the User Switching old user cookie is set.
*
* @since 1.4.0
*
* @param string $olduser_cookie The old user cookie value.
* @param int $expiration The time when the logged-in authentication cookie expires as a UNIX timestamp.
* @param int $old_user_id User ID.
* @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'.
* @param string $token User's session token to use for this cookie.
*/
do_action( 'set_olduser_cookie', $olduser_cookie, $expiration, $old_user_id, $scheme, $token );
/**
* Allows preventing auth cookies from actually being sent to the client.
*
* @since 1.5.4
*
* @param bool $send Whether to send auth cookies to the client.
*/
if ( ! apply_filters( 'user_switching_send_auth_cookies', true ) ) {
return;
}
setcookie( $auth_cookie_name, $auth_cookie, $expiration, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_auth_cookie, true );
setcookie( USER_SWITCHING_OLDUSER_COOKIE, $olduser_cookie, $expiration, COOKIEPATH, COOKIE_DOMAIN, $secure_olduser_cookie, true );
}