[User Plugin] Users Login

Hi not sure you can help, I would like to do the following
Display a list of users via the frontend with a button or text to enable me to login into their account

  • Adam Czajczyk
    • Support Gorilla

    Hi Chez

    I hope you're well today and thank you for your question!

    I admit I'm not quite sure if I correctly understand your request so let's make sure that we're on the same side :slight_smile:

    There's a plugin that let's you "login as any user" if you're already logged in as an admin. You'll find it here:

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

    However, it doesn't give any list on the front-end and this, actually, is a part that confuses me a bit. It would be quite easy to put a list of all users on the front-end (e.g. via a custom created shortcode) but what about

    - the list visibility - should it be visible all the time/for everyone or only for the admin users?
    - the login - this should only work if you are already logged in as administrator (like that User Switching plugin) correct?

    Allowing that to any other user role than admin would be a serious security threat in my opinion.

    That said, could you please address my doubts above? I'll then look into possible solutions for you.

    Best regards,
    Adam

  • Chez
    • Flash Drive

    Hi sorry I did not explain very well.

    All the users within the database will never access the site, the site is for one company who will add the orders themselves, the customers being the users (one person with admin rights will add orders ).

    The website is being used to process orders, send delivery notes, proforma's etc, but not invoicing (hoping to get it that once an order is complete a delivery document will be sent to customers and accounts so they know to invoice), this is being carried out in the accounts package, its basically a way of keeping inventory on the products.

    I'm trying to make it easy for the admin user to add orders, using a bulk order plugin, which gets added to cart, and at some point when the order is complete pdf's will be sent.

    Ideally I want to make it easy for the admin user to switch from customer to customer to add the orders from the front end, I was trying to do it from the back-end so no login is needed, however I know they will find it difficult process the order.

    So to conclude on the website somewhere, perhaps a drop down or something, listing all users, which can be selected, and then log in as that user to add order.

    Hope that makes sense, thank you

  • Adam Czajczyk
    • Support Gorilla

    Hi Chez

    Thank you for additional explanation!

    I think this plugin should work just fine for you then:

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

    I just tested it on my site and what it does is it adds a "slide in" on the side of the site on front-end that contains a list of all users; by clicking on the user name you just switch to that user.

    Note: if you got WP_DEBUG (WordPress debuggin) enabled the "switcher" will be visible for everyone; with it set to "false" (disabled) only logged in users can switch accounts.

    There's one downside of the plugin though: it allows any logged in user switch to any other user, without a password. This means that any subscriber can, for example, switch to an admin which is a big security threat.

    If the site is not publicly available (so it's some "closed environoment"/intranet) and only users that have access to it are admins anyway, that would be fine. But if there are different levels of access and/or the site is exposed publicly - that wouldn't be acceptable in my opinion.

    That could probably be "tweaked", for example not to show the "switcher" for non-admin members (in fact there's even a filter that would allow this, if I'm not mistaken) but then after switching to subscriber user an admin wouldn't be able to switch back, I think. But still - if not in current form (unless aforementioned downsides are not an issue for you), this plugin seems like quite a good "foundation" to custom-build upon it :slight_smile:

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hi Chez

    I checked the plugin's code again and I think it's not doable without changing it. It's never recommended to change plugin's code directly as all the changes would be lost upon any update to the plugin and there's always a chance that update will change the code too much to make it possible to re-apply customization.

    If that risk is acceptable to you, then it can be done. In general, there's a code in the

    /wp-content/plugins/switch-user/modules/frontend/class-switch-user-frontend.php

    file that displays these usernames. Around line 171 in that file, there's this code block:

    foreach ( $users as $user ) {
                            if ($user->ID == $current_user_id ) {
                                 echo '<li class="current-user" data-user-id="' . $user->ID . '" title="' . __("You are logged as this user", SWITCH_USER_TEXTDOMAIN) . '">' . $user->user_login . '</li>';
                            } else {
                                 echo '<li class="js-su-user" data-user-id="' . $user->ID . '" title="' . __("Click to login as this user", SWITCH_USER_TEXTDOMAIN) . '">' . $user->user_login . '</li>';
                            }
                        }

    This specific part of it (both instances) is responsible for showing user login there:

    $user->user_login

    This can be replaced to show something else and the plugin will still work fine but what to replace it with and whether some additional code is necessary or not - that depends where exactly that "company name" would be coming from. Since you didn't answer my question about that, I don't really know what code I should suggest to display what you want.

    Assuming that this company name is just a regular user profile filed, that code block above could be modified like this:

    foreach ( $users as $user ) {
    
    	$company_name = get_user_meta( $user->ID, 'company_name', true );
    
        if ($user->ID == $current_user_id ) {
             echo '<li class="current-user" data-user-id="' . $user->ID . '" title="' . __("You are logged as this user", SWITCH_USER_TEXTDOMAIN) . '">' . $company_name . '</li>';
        } else {
             echo '<li class="js-su-user" data-user-id="' . $user->ID . '" title="' . __("Click to login as this user", SWITCH_USER_TEXTDOMAIN) . '">' . $company_name . '</li>';
            }
        }

    But please note: the code above will only work out of the box for you
    - if the "company name" is indeed user profile field
    - and the field name (meta_key name in the db) is "company_name"

    So most likely that needs additional modification but I'd need to know exactly how/where that company name is stored :slight_smile:

    Best regards,
    Adam

  • Chez
    • Flash Drive

    Hi
    Thank you so much for your help and sorry for not answering your question. You have helped and it has worked:
    foreach ( $users as $user ) {

    $billing_company = get_user_meta( $user->ID, 'billing_company', true );

    if ($user->ID == $current_user_id ) {
    echo '<li class="current-user" data-user-id="' . $user->ID . '" title="' . __("You are logged as this user", SWITCH_USER_TEXTDOMAIN) . '">' . $billing_company . '';
    } else {
    echo '<li class="js-su-user" data-user-id="' . $user->ID . '" title="' . __("Click to login as this user", SWITCH_USER_TEXTDOMAIN) . '">' . $billing_company . '';
    }
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.