johnbillion / user-switching

WordPress plugin that provides instant switching between user accounts.
https://wordpress.org/plugins/user-switching/
GNU General Public License v2.0
187 stars 49 forks source link

Custom Integration #102

Closed mattpramschufer closed 1 year ago

mattpramschufer commented 1 year ago

I am developing a custom plugin which I would like to interact with your plugin. I have a member report that I would like admins to be able to switch to that user from the custom report screen. I see in the code you have a public static function for generating the URL.

I have incorporated that into my report screen as follows

<a href="<?php echo esc_url( user_switching::maybe_switch_url($member['user'])); ?>">Switch to</a>

This works great and does generate the following link http://localhost:10003/wp-login.php?action=switch_to_user&user_id=425&nr=1&_wpnonce=e81c664395

The issue is when I actually goto that link, it just takes me to the Login screen with the notice at the top to switch back to matt, my admin account. I was just curious if I was missing something. I know this is not an issue with your plugin at all, I just figured i'd ask.

Thanks in advance!

johnbillion commented 1 year ago

Are you currently switched into another account while doing your testing? Does the switching work ok when you switch from the Users screen in the WordPress admin area? If you compare the "Switch To" URLs they should be identical. The maybe_switch_url() method is the correct one to use.

johnbillion commented 1 year ago

Oh wait, when you use the link have you successfully switched to the target user? If so, then the issue is just with the redirect. Maybe the user you're switching to doesn't have any capabilities. You can tack on a &redirect_to= query parameter to the switch URL. There's documentation for this somewhere in the plugin FAQ.

mattpramschufer commented 1 year ago

Yea the URLs are the exact same, and I thought the same thing with the redirect, but that isn't the case. If you are saying that is the function that should work, then that is all I needed to hear. I believe it is on my end and I have some session issues going on from migrating from production down to local. I do not want to waste your time as I debug my plugin. I just wanted to make sure what I was doing was correct. Thanks so much for the quick response and your plugin is a must-have on all of my sites.

johnbillion commented 1 year ago

Yeah in that case it might be a caching or session issue related to the front end. Try deactivating your object cache etc for testing. Best of luck!

mattpramschufer commented 1 year ago

@johnbillion I can confirm it was 100% on my end. I just pushed to production and tested and it worked perfectly using the following

<a href="<?php echo esc_url( user_switching::maybe_switch_url($member['user'])); ?>&redirect_to=<?php echo urlencode(site_url('/account/', 'https')); ?>">Switch to</a>

Thanks again.