• Resolved scottywic

    (@scottywic)


    Hi John, amazing plugin.
    I’m running into an issue with the user switching. I can switch from the admin but when i generate the url and use that it is erroring out and saying are you sure you want to do this.
    I believe this is because wp nonce isn’t validating, but I’m not sure why.

    Here is the code I’m using to display the results.

    if ($linkin){
    				echo('<h2>Active Linked Accounts</h2><br/>');
    			foreach ($linkin as $link_user_id) {
    				if (in_array($link_user_id, $linksent)) {
        			$link_user = get_user_by( 'id', $link_user_id );
    				$link_user_name = $link_user->first_name;
    				$link_last_name = $link_user->last_name;
    				//switch to url generation
    				$url = wp_nonce_url( add_query_arg( array(
    				'action'  => 'switch_to_user',
    				'user_id' => $link_user_id,
    				), wp_login_url() ), "switch_to_user_{$user->ID}" );
    
    				echo('<a href="'.$url.'"><h3>' .$link_user_name. ' ' .$link_last_name. '</h3>Switch to this User</a><br/>');
    				}
    			} } else {
    				echo('<h2>Active Linked Accounts</h2><br/>None currently.');
    			}

    And a sample url it’s spitting out is
    https://snobahn.com/wp-login.php?action=switch_to_user&user_id=118886&_wpnonce=4fab6652aa

    Any thoughts as to how I can get these to switch up?

    https://www.ads-software.com/plugins/user-switching/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    Hi Scotty,

    From a glance at your code, it looks like $user->ID isn’t what you’re expecting it to be. You’ll need to use $link_user_id instead.

    In addition, there’s a function in User Switching you can use instead of manually constructing the URL: user_switching::switch_to_url( $user ). That way you don’t need to worry about nonces, etc.

    $url = user_switching::switch_to_url( $link_user );
    echo '<a href="'.$url.'"><h3>' .$link_user_name. ' ' .$link_last_name. '</h3>Switch to this User</a><br/>';

    There’s also a context-aware function called user_switching::maybe_switch_url( $user ) which outputs a switch to or switch back link as appropriate, but for your use case you don’t need that.

    Thread Starter scottywic

    (@scottywic)

    wow man you’re awesome. That did it and that was ultra fast.
    Anywhere I can donate to your efforts?

    Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    Cool, glad it worked.

    I don’t accept donations, but reviews are appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘User switching – Are you sure you want to do this error.’ is closed to new replies.