• Resolved tanasi

    (@tanasi)


    Hi!

    At first, this is a great plugin. I was very impressed by the simplicity and the effectiveness of it not only for switching between user profiles but also for managing roles! I wonder why this is not popular really.

    One question: I have a custom role “seller” in my woocommerce store and I want this role to be able to login as a customer to help him buy products.
    In seller role capabilities I have enabled only the “edit_users” and the 2 view_admin_as permissions as indicated in other posts in this forum.

    So, far so good. The seller can switch to any customer and this is amazing.
    BUT, the seller can switch also as an shopmanager and this is not good, as the shopmanager has many permissions that shouldn’t be active on seller.

    Is it possible to limit the seller role to specific roles?

    I have tried to play with the level_0, level_1 etc but with no luck.
    Is there any hook or any way to achieve this?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @tanasi

    Thank you! And yes, this is definitely possible.
    In fact, it’s always recommended to limit the capabilities if you are allowing users to create/edit users.

    Luckily, WordPress core has a built-in filter for this: editable_roles: https://developer.www.ads-software.com/reference/hooks/editable_roles/

    Example (see also level_# example in doc page above):

    add_filter( 'editable_roles', function( $roles ) {
        $user = wp_get_current_user();
    
        // Define your logic to enable/disable role edits for the current user.
        // For example:
        if ( in_array( 'seller', $user->roles ) ) {
            $roles = [ 'subscriber', 'editor' ]; // Only allow viewing subscriber and editor.
        }
        
        return $roles;
    } );

    Note that some plugins allow you to add multiple roles to a user, this will require you to improve the code above to validate all the user roles instead of just one.

    Hope this helps!

    Cheers, Jory

    Thread Starter tanasi

    (@tanasi)

    Hi,

    thanks for the detailed reply.

    I tried your example but I don’t get to make it work.

    I have deactivated all plugins, activated the base theme, put the code to the functions.php, but the seller role still can see and switch as shop manager.

    I have tried also to set the seller role as level_0 and the put the shopmanager as level_5 and use the respective code from the link https://developer.www.ads-software.com/reference/hooks/editable_roles/
    but no luck.

    Any ideas on what else I can try?

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @tanasi

    Sorry, I always forget to mention that filters made for this plugin should be added as a plugin or even better, as a mu-plugin. With the latter you can just put the same code in a PHP file and put it a /wp-content/mu-plugins.

    Since the code for VAA runs so early, adding it in your theme will be to late as all validations have run already.

    It might be a nice addon to the plugin to be able to edit them using the UI.

    Cheers, Jory

    Thread Starter tanasi

    (@tanasi)

    Hmm I tried your suggestion but I can’t make it work.

    I have tried both in plain plugin and in mu-plugin as you suggested.

    In that plugin I have added the code you provided and also added another code to show a text in the footer so that I know that my plugin is active and working.

    The seller role can still switch to shopmanager role.

    I have also tried to recreate that in a clean environment with only woocommerce and VAA and still the seller can switch.

    Any ideas?

    (I can provide creds if you like to check it)

    Plugin Author Jory Hogeveen

    (@keraweb)

    Could you share the code you’ve used in your mu-plugin?

    Creds are not allowed to be shared through the forums.

    Cheers, Jory

    Thread Starter tanasi

    (@tanasi)

    The code is:

    add_filter( 'editable_roles', function( $roles ) {
        $user = wp_get_current_user();
    
        // Define your logic to enable/disable role edits for the current user.
        // For example:
        if ( in_array( 'seller', $user->roles ) ) {
            $roles = [ 'subscriber', 'editor' ]; // Only allow viewing subscriber and editor.
        }
        
        return $roles;
    } );
    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @tanasi

    Ah you’ve just copied the code. Please make sure that the names for these roles are 100% correct. I’ve written them based on your writings but it could well be that the internal name is different.

    Edit: You did add a <php to the file right? And I also see an HTML entity in your copied code. The code was meant as an example, you should changed it to match your actual installation.

    Also, note that your need to login as a Seller role user in order to see the changes in your View Admin As menu.

    Cheers, Jory

    • This reply was modified 6 months, 3 weeks ago by Jory Hogeveen.
    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @tanasi

    I also just noticed an issue with my code, apparently the roles are set as the array keys.
    New code (this time the php prefix)

    <?php
    add_filter( 'editable_roles', function( $roles ) {
    	$user = wp_get_current_user();
    	$editable_roles = [];
    
    	// Define your logic to enable/disable role edits for the current user.
    	// For example:
    	if ( in_array( 'seller', $user->roles ) ) {
    		$editable_roles = [ 'subscriber', 'editor' ]; // Only allow viewing subscriber and editor.
    	}
    
    	if ( $editable_roles ) {
    		return array_intersect_key( $roles, array_flip( $editable_roles ) );
    	}
    
    	return $roles;
    } );

    Hope this helps!

    Cheers, Jory

    Thread Starter tanasi

    (@tanasi)

    Hi Jory,

    this seems to work great now!!

    Yes I did as you said before (correct names, php at the beginning etc) but something was missing I guess.

    Keep up the good work.. Your plugin is amazing ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Use plugin on a custom role but limit on shopmanager’ is closed to new replies.