WordPress.org

Ready to get started?Download WordPress

Forums

User Switching
[resolved] Add Admin Bar User Switching (10 posts)

  1. landwire
    Member
    Posted 4 months ago #

    Hi John,

    would it make sense to add this functionality to your plugin rather then have to install an additional plugin? And then rather put it into the admin bar "avatar menu dropdown" on the right (where your plugin has it's "switch back" link). That would make sense to me.

    Also when switching at the moment with the separate add on (from the link below) it takes you to the home page of your blog. Ideally that would directly redirect to the page you were on and not go to the home page.

    http://wordpress.org/support/topic/user-switching-from-admin-bar?replies=6

    And many thanks for an invaluable, absolute fantastic plugin!
    Sascha

    https://wordpress.org/plugins/user-switching/

  2. landwire
    Member
    Posted 4 months ago #

    And if you do not want to add it to your main plugin:

    Is there a way to redirect to the current page, that is displayed when you make a switch, if your plugin is used on the front end?

    Sascha

  3. John Blackbourn
    Member
    Plugin Author

    Posted 4 months ago #

    Hi Sascha,

    I won't be adding this functionality into the core User Switching plugin. It's a handy plugin but I don't think it's of use to the majority of users, and also on sites with a large number of users it soon becomes slow. I want to keep User Switching as light as possible.

    You can choose where to redirect after switching by adding a redirect_to parameter to the switch link. This change should do it: https://github.com/johnbillion/admin-bar-user-switching/commit/a4c13816463436d1bfee74ef169a3ca3c9fecbfd

    John

  4. landwire
    Member
    Posted 4 months ago #

    John,

    thanks a billion :) Works like a charm! I would have had no idea where to start. Just a single line!

    Would you know how to add it to the same menu as you add your "Switch Back" too? Just so the admin bar does not get so cluttered. You don't need to code it for me and to be honest it is not that crucial. But I assume it would be simple enough and just completes the add on for your plugin nicely.

    BTW: is there a way to get rid of the "?user_switched=true" and "?user_switched=true&switched_back=true" at the end of the url when switching and switching back? Again, not at all crucial, but I was thinking of maybe trying to implement user switching on the front end for users (I will have "individual" users, that can create an "office" user and manage that, so they are linked together and should be able to simply switch between them) and then the url should be "clean".

    Obviously I would remove the second "Switch Back" link. But as said, I can do that. I can code a little, but some things are just over my head still.
    And don't worry, if you don't want to get your head full of this at all. I feel like you gave me your little finger and I am grabbing your whole hand (it's a German saying - no idea how to translate it right now).
    S

  5. John Blackbourn
    Member
    Plugin Author

    Posted 4 months ago #

    BTW: is there a way to get rid of the "?user_switched=true" and "?user_switched=true&switched_back=true" at the end of the url when switching and switching back?

    These query variables are added here: https://github.com/johnbillion/user-switching/blob/master/user-switching.php#L157-L160. If you really want to remove them then remove them from there, but you'll need to remember to remove it if you update the plugin in the future.

    John

  6. landwire
    Member
    Posted 4 months ago #

    Ok, thanks yet again.

    Do you think it's safe to let logged in users switch on the front end?

    Scenario:
    I have a design professional register as user professional. He gets the role: professional.

    Then he adds his design office as a new user (role: design_office). But he also is head of a design research department and creates an user account for that department (role: research_department).

    When he logs in as a professional I want him to be able to switch to design_office account or research_department account if he wants to do that. And obviously switch back. I would basically modify your plugin and change the user queries to only show the connected accounts (I use p2p plugin by scribu to connect users to users) for that currently logged in user.

    Anyway, before I start on a big programming/modifying spree I just thought I ask your 2 cents about it.
    S

  7. landwire
    Member
    Posted 4 months ago #

    I am not 100% sure about your function filter_user_has_cap(), but I assume $args[2] contains the user_id I want to switch to?

    Looking at your code likely I would just change the following:
    https://github.com/johnbillion/user-switching/blob/master/user-switching.php#L532

    $user_caps['switch_to_user'] = ( user_can( $args[1], 'edit_user', $args[2] ) and ( $args[2] != $args[1] ) and ( is_connected( $args[1], $args[2] ) ) );

    Obviously that only works if I have both current_user and maybe_switch_to_user IDs available.

  8. John Blackbourn
    Member
    Plugin Author

    Posted 4 months ago #

    Unfortunately I just don't have the time to provide support to this degree Sascha.

    $args[2] is indeed the user ID you want to switch to. As you'll see from the inline docs for that method it's the optional second parameter from current_user_can() which is called like this: current_user_can( 'switch_to_user', $user_id ). Hope that helps.

  9. landwire
    Member
    Posted 4 months ago #

    All helps. Many Thanks! I think I will get there from here on.
    Your plugin is very well documented and I am getting more and more the hang of WordPress and PHP.

    And I totally understand! Amazing that you provide support to the level that you do already! Thanks again,
    S

    And just now I learned another thing about how filters work. It's all pretty complex with WordPress. Just wish they would list all the filters with each function.

  10. landwire
    Member
    Posted 4 months ago #

    Ok, got it all working! Thanks for your guidance again with removing the query variables and changing the redirect to the current page! I would have likely spent some days on it.

    If I ever finish this platform I am trying to create and it makes some money then I would really like to give a donation. For now I am rather poor :(

    I know I have seen somewhere that you do not take donations, but maybe you will one day use a flattr account or something like that. If you do or change your mind, then let me know :)
    Sascha

Reply

You must log in to post.

About this Plugin

About this Topic