Title: Calling the maybe_switch_user function
Last modified: June 16, 2022

---

# Calling the maybe_switch_user function

 *  Resolved [g4macgregor](https://wordpress.org/support/users/g4macgregor/)
 * (@g4macgregor)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/calling-the-maybe_switch_user-function/)
 * We have built a function that uses the `woocommerce_shop_order_search_results`
   hook to search for something in a custom table that then returns the `customer_id`,
   get the `WP_User object` for that `id` and are attempting to use javascript `
   confirm()` to call `maybe_swith_user` but are getting the ‘Could not switch users’
   response. We’re thinking it is because it’s rebuilding the DOM and we don’t have
   the necessary info for `old_user`, but aren’t sure.
 *     ```
       if ( method_exists( 'user_switching', 'maybe_switch_url' ) ) {
            $url = user_switching::maybe_switch_url( $target_user );
            if ( $url ) {
                echo "<script>if ( confirm('This is a historical Opencart serial number. Click OK to jump to his customer') ) document.location='$url';</script>";
            }
       }
       ```
   
 * Is there a way you can think of to make this work?
 * Thanks

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [g4macgregor](https://wordpress.org/support/users/g4macgregor/)
 * (@g4macgregor)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/calling-the-maybe_switch_user-function/#post-15744620)
 * Additional information:
 * When we write log from the plugin script, we’re seeing:
 * [16-Jun-2022 20:06:44 UTC] Array
    ( [action] => switch_to_user [amp;user_id] 
   => 78091 [amp;nr] => 1 [amp;_wpnonce] => c4cbd18580 )`
 * Not sure why it’s prefacing the amp;
    -  This reply was modified 3 years, 11 months ago by [g4macgregor](https://wordpress.org/support/users/g4macgregor/).
    -  This reply was modified 3 years, 11 months ago by [g4macgregor](https://wordpress.org/support/users/g4macgregor/).
 *  Plugin Author [John Blackbourn](https://wordpress.org/support/users/johnbillion/)
 * (@johnbillion)
 * WordPress Core Developer
 * [3 years, 11 months ago](https://wordpress.org/support/topic/calling-the-maybe_switch_user-function/#post-15744698)
 * I’m not sure what could be the problem but you probably should reduce your code
   down as much as you can to eliminate potential problems. For example replace 
   the JavaScript with a regular `<a href="...">` that you output with PHP and then
   test that it works ok when you click it.
 *  Thread Starter [g4macgregor](https://wordpress.org/support/users/g4macgregor/)
 * (@g4macgregor)
 * [3 years, 11 months ago](https://wordpress.org/support/topic/calling-the-maybe_switch_user-function/#post-15744706)
 * Found it, thanks! We needed to `html_entity_decode($url)`
    -  This reply was modified 3 years, 11 months ago by [g4macgregor](https://wordpress.org/support/users/g4macgregor/).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Calling the maybe_switch_user function’ is closed to new replies.

 * ![](https://ps.w.org/user-switching/assets/icon.svg?rev=3193956)
 * [User Switching](https://wordpress.org/plugins/user-switching/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/user-switching/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/user-switching/)
 * [Active Topics](https://wordpress.org/support/plugin/user-switching/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/user-switching/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/user-switching/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [g4macgregor](https://wordpress.org/support/users/g4macgregor/)
 * Last activity: [3 years, 11 months ago](https://wordpress.org/support/topic/calling-the-maybe_switch_user-function/#post-15744706)
 * Status: resolved