• Resolved Jonas Lundman

    (@jonas-lundman)


    Hi, and thanks for this plugin

    Im trying to add a “switch to” link in a custom post type admin table column. I manage to create the link but I have problems with the check_admin_referer() stuff.

    I dont understand the nonce – and I propably missing something. This is my function call in the table column where my users are shown:

    $u = The user_id

    function ua_user_switch($u){
    	global $user_switching;
    	$link = $user_switching->switch_to_url(switch_to_user( $u ));
    	if(!$link) return false;
    	echo '<a href="'.esc_url($link).'">'.esc_html( 'Switch&nbsp;To', 'user-switching' ).'</a>';
    }

    It works if I comment out this:

    `# Check intent:
    check_admin_referer( “switch_to_user_{$user_id}” );`

    Could someone help me out?

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

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

    (@johnbillion)

    WordPress Core Developer

    Hey Jonas,

    On the third line of your code, you have a call to switch_to_user( $u ). This is incorrect because this is the function which actually switches user.

    Secondly, maybe_switch_url( $user ) is a better method to use in place of switch_to_url( $user ) because it handles users trying to switch to themselves and users who’ve already switched.

    With that in mind, here’s the code I would use:

    function ua_user_switch($u){
    	global $user_switching;
    	$user = get_userdata( $u );
    	$link = $user_switching->maybe_switch_url( $user );
    	if(!$link) return false;
    	echo '<a href="'.esc_url($link).'">'.esc_html__( 'Switch&nbsp;To', 'user-switching' ).'</a>';
    }

    John

    Thread Starter Jonas Lundman

    (@jonas-lundman)

    Hi
    Thanks for a quick answer! And the suggestion worked!

    Is there any way to control the page you get redirected to when the swithing is made?

    Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    Yes, you can add a URL-encoded redirect_to parameter to the link and the user will get redirected to that URL after switching.

    Example:

    function ua_user_switch($u){
    	global $user_switching;
    	$user = get_userdata( $u );
    	$link = $user_switching->maybe_switch_url( $user );
    	if(!$link) return false;
    	$link = add_query_arg( 'redirect_to', urlencode( home_url( 'hello' ) ), $link );
    	echo '<a href="'.esc_url($link).'">'.esc_html__( 'Switch&nbsp;To', 'user-switching' ).'</a>';
    }
    Thread Starter Jonas Lundman

    (@jonas-lundman)

    Thanks, works great, and for Google answers for this thread, using this plugin with woocommmerce ( switch to customers order (history)) when y also have a local store together with online bizz.

    Closing!

    Thanks for a great plug!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom -switch to- link’ is closed to new replies.