switch_to_user
Fires when a user switches to another user account. Fires when a user switches accounts, passing IDs of the old and new users and their session tokens.
add_action( 'switch_to_user', $callback, 10, 4 );
Description
Fires after a user switches to another account via the user switching feature. Developers can use this hook to perform actions related to the switch, such as logging the event, updating user-specific settings, or clearing caches. It provides the IDs and session tokens of both the old and new users.
Usage
add_action( 'switch_to_user', 'your_function_name', 10, 4 );
Parameters
-
$user_id(int) - The ID of the user being switched to.
-
$old_user_id(int) - The ID of the user being switched from.
-
$new_token(string) - The token of the session of the user being switched to. Can be an empty string or a token for a session that may or may not still be valid.
-
$old_token(string) - The token of the session of the user being switched from.
Examples
<?php
/**
* Log user switching events to a custom log file for auditing purposes.
*
* This function hooks into the 'switch_to_user' action and records
* details about the user switch, including the user IDs and session tokens.
*
* @param int $user_id The ID of the user being switched to.
* @param int $old_user_id The ID of the user being switched from.
* @param string $new_token The token of the session of the user being switched to.
* @param string $old_token The token of the session of the user being switched from.
*/
function my_custom_user_switch_logger( $user_id, $old_user_id, $new_token, $old_token ) {
$log_file = WP_CONTENT_DIR . '/user-switch.log';
$timestamp = current_time( 'mysql' );
// Get user display names for better logging
$current_user = get_userdata( $user_id );
$previous_user = get_userdata( $old_user_id );
$current_username = $current_user ? $current_user->user_login : 'N/A';
$previous_username = $previous_user ? $previous_user->user_login : 'N/A';
$log_message = sprintf(
"[%s] User switched. From User ID: %d ('%s') to User ID: %d ('%s'). Old Token: '%s', New Token: '%s'n",
$timestamp,
$old_user_id,
$previous_username,
$user_id,
$current_username,
$old_token,
$new_token
);
// Append the log message to the file
file_put_contents( $log_file, $log_message, FILE_APPEND );
}
add_action( 'switch_to_user', 'my_custom_user_switch_logger', 10, 4 );
?>
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:1534
*
* @param int $user_id The ID of the user being switched to.
* @param int $old_user_id The ID of the user being switched from.
* @param string $new_token The token of the session of the user being switched to. Can be an empty string
* or a token for a session that may or may not still be valid.
* @param string $old_token The token of the session of the user being switched from.
*/
do_action( 'switch_to_user', $user_id, $old_user_id, $new_token, $old_token );
} else {
/**
* Fires when a user switches back to their originating account.
*
* @since 0.6.0
* @since 1.4.0 The `$new_token` and `$old_token` parameters were added.
*