Action Since 1.4.0 The `$new_token` and `$old_token` parameters were added. uncanny-learndash-toolkit

switch_back_user

Fires when a user switches back to their originating account. Fires when a user switches back to their original account after being impersonated.

add_action( 'switch_back_user', $callback, 10, 4 );

Description

Fires after a user switches back to their original account. Developers can hook into this action to perform tasks like updating user meta, logging switch-back events, or re-initializing specific user settings. The `$user_id` and `$old_user_id` parameters provide context about the accounts involved in the switch.


Usage

add_action( 'switch_back_user', 'your_function_name', 10, 4 );

Parameters

$user_id (int)
The ID of the user being switched back to.
$old_user_id (int|false)
The ID of the user being switched from, or false if the user is switching back after having been switched off.
$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

/**
 * Logs the user switch back event to a custom log file.
 *
 * This function is triggered when a user switches back to their original account
 * after impersonating another user. It records the details of the switch,
 * including the user IDs and session tokens involved, to aid in debugging and
 * auditing user activity.
 */
function log_user_switch_back( $user_id, $old_user_id, $new_token, $old_token ) {
	// Ensure we are only logging when a switch back actually occurs
	// and not for initial logins or other token-related events.
	if ( $old_user_id && $old_user_id !== $user_id ) {
		$timestamp = current_time( 'mysql' );
		$log_message = sprintf(
			"[%s] User switched back. New User ID: %d, Old User ID: %d, New Token: %s, Old Token: %sn",
			$timestamp,
			absint( $user_id ),
			absint( $old_user_id ),
			sanitize_text_field( $new_token ),
			sanitize_text_field( $old_token )
		);

		// Define the path to your custom log file.
		// It's recommended to place this outside of your web root for security.
		$log_file_path = WP_CONTENT_DIR . '/user-switch-back.log';

		// Use file_put_contents for simplicity, ensuring it's appended.
		// Check if the file exists and is writable, otherwise, log an error.
		if ( is_writable( dirname( $log_file_path ) ) ) {
			file_put_contents( $log_file_path, $log_message, FILE_APPEND | LOCK_EX );
		} else {
			// Fallback to error_log if the custom log file isn't writable.
			error_log( "User Switch Back Log: Failed to write to {$log_file_path}. User IDs: {$user_id}, {$old_user_id}" );
		}
	}
}
add_action( 'switch_back_user', 'log_user_switch_back', 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:1549

* @param int       $user_id     The ID of the user being switched back to.
			 * @param int|false $old_user_id The ID of the user being switched from, or false if the user is switching back
			 *                               after having been switched off.
			 * @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_back_user', $user_id, $old_user_id, $new_token, $old_token );
		}

		if ( $old_token && $old_user_id && ! $set_old_user ) {
			// When switching back, destroy the session for the old user
			$manager = WP_Session_Tokens::get_instance( $old_user_id );
			$manager->destroy( $old_token );
		}


Scroll to Top