I have spent the last two hours searching for this and I cannot find a solution to my problem.

I have a WP site with WooCommerce running which allows to users to create their own store.

My client has a scenario he needs two stores, but without having to login and logout every time to check this data.

I've already checked "User Switching" but its functionality is limited.

  1. It only allows you to switch to a user from the backend, which doesn't help because the access to WP Dashboard is locked.
  2. I found a repo which adds User Switch to the WP Admin bar. The WP Admin bar though is not visible to users. Even if I keep the bar open the problem is that it allows you to search through all users and get access to any account you want, but I need the user to be able to only switch between specific accounts.

I searched for other solutions but I cannot find anything out there.

Any suggestions would be greatly appreciated!

  • 1
    Why would he need 2 users though? Surely if he logs in under the 2nd username, he'll just see exactly the same stuff but with different information in the profile page? – Tom J Nowell Jun 9 '15 at 0:17
  • @TomJNowell It's has more to do with WooCommerce data. One store contains certain types of things he can do and the other has different options as well. – kat_indo Jun 9 '15 at 0:26
  • That still makes little sense, are you talking about 2 separate installs of WordPress? If I install WooCommerce and create 2 users, both users can see the same thing when they log in, I think you're missing a pivotal piece of information here. If they're not separate stores, how are the users different? ( in a technical capacity ) Do you have code that limits what a user can see? ( if so can you edit your question to put it in? ) Is each store a site in a multisite install? – Tom J Nowell Jun 9 '15 at 0:41
  • No, the way our setups works is that each user can also own a store, when you own a store you are allowed to add products, access reports, etc. So we need them to have two accounts so they can own two stores, but an easy way to access both. Do you think there's another solution to the problem? – kat_indo Jun 9 '15 at 0:43
  • I still don't understand, please define what a 'store' is, is a store a blog on a multisite? Do you have multiple installs of WordPress were each install is for a single user? Is a store a top level product category? I asked multiple questions in my comment but you've ignored most of them :( – Tom J Nowell Jun 9 '15 at 0:46

I would reverse engineer the User Switching plugin and then create a solution that works for you. Fortunately there is not a whole lot of work to do. Most of the crucial functionality is in this function:

function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
    if ( ! $user = get_userdata( $user_id ) ) {
        return false;
    }

    $old_user_id = get_current_user_id();

    if ( $set_old_user && $old_user_id ) {
        user_switching_set_olduser_cookie( $old_user_id );
    } else {
        user_switching_clear_olduser_cookie( false );
    }

    wp_clear_auth_cookie();
    wp_set_auth_cookie( $user_id, $remember );
    wp_set_current_user( $user_id );

    if ( $set_old_user ) {
        do_action( 'switch_to_user', $user_id, $old_user_id );
    } else {
        do_action( 'switch_back_user', $user_id, $old_user_id );
    }

    return $user;
}

You could just maintain a list of "linked accounts" for each user to determine if a user is allowed to switch to another account.

Pseudo code:

$switch_to = 0; // some user id
$linked_accounts = get_linked_accounts( get_current_user_id() );
if(in_array($switch_to, $linked_accounts)) {
    switch_to_user($switch_to)
}

You would need to define get_linked_accounts and setup a way to actually do the linking. You should also do a lot of testing to make sure users will not be able to switch to any arbitrary account.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.