WordPress action hook
switch_to_user
Fired by 3 WordPress plugins, running on 221K+ sites between them.
“Fires when a user switches to another user account.”
How to use switch_to_user
It's an action: your callback runs at that point and receives 4 arguments. Nothing needs to be returned.
Add this to your theme's functions.php or a small custom plugin:
add_action( 'switch_to_user', function( $user_id, $old_user_id, $new_token, $old_token ) {
// Your code runs here.
}, 10, 4 );Argument names come from User Switching's source. Other plugins may pass different values: check the call in the plugin you're targeting below.
3 plugins that fire switch_to_user
Sorted by active installs. Each entry shows where the plugin calls the hook and what it passes.
-
User Switching200,000+ installs
do_action( 'switch_to_user', $user_id, $old_user_id, $new_token, $old_token )user-switching.php:1674 -
Uncanny Toolkit for LearnDash20,000+ installs
do_action( 'switch_to_user', $user_id, $old_user_id, $new_token, $old_token )src/includes/user-switching.php:1687 -
Login Me Now600+ installs
do_action( 'switch_to_user', $user_id, $old_user_id, $new_token, $old_token )app/Logins/UserSwitchingLogin/user-switching.php:1444
Need a customisation built on this hook?
Tell us what you want to change and which plugin it's for. We'll write it, test it and send it back as a small plugin.