I’ve approached this slightly differently …
My preference would be to expose the switching URL as a filter …
/**
* Returns the nonce-secured URL needed to switch to a given user ID.
*
* @param WP_User $user The user to be switched to.
* @return string The required URL.
*/
public static function switch_to_url( WP_User $user ) {
return apply_filters( 'switch_to_url', wp_nonce_url( add_query_arg( array(
'action' => 'switch_to_user',
'user_id' => $user->ID,
'nr' => 1,
), wp_login_url() ), "switch_to_user_{$user->ID}" ), $user );
}
Then you can just do
add_filter( 'switch_to_url', 'my_switch_to_url', 9, 2 );
function my_switch_to_url($url, $user) {
//your conditions here
return $url;
}
-
This reply was modified 2 years, 5 months ago by
bencoates.