• Chris

    (@christof15)


    Hi there, thank you so much for this great plugin!

    I’ve added a user role ‘dailyadmin’ based of the administrator role for daily admin tasks. They are allowed to add/edit users etc. Is there a way to hide the top level administrator user role from the dropdown selection? So the daily admin cannot pick the top level administrator role for the new user.

    Thank you in advance!

    I was already able to completely hide the ‘Additional Capabilities’ section using your code snippet from here and also the top level administrator from the user list using this code:

    // START HIDE USER BASED ON USERNAME
    /* Hide user based on username */
    add_action('pre_user_query','site_pre_user_query');
    function site_pre_user_query($user_search) {
      global $current_user;
      $username = $current_user->user_login;
     
      if ($username == 'USERNAME') {
      } else {
        global $wpdb;
        $user_search->query_where = str_replace('WHERE 1=1',
          "WHERE 1=1 AND {$wpdb->users}.user_login != 'USERNAME'",$user_search->query_where);
      }
    }
    /* Display correct amount of users */
    add_filter("views_users", "site_list_table_views");
    function site_list_table_views($views) {
       $users = count_users();
       $admins_num = $users['avail_roles']['administrator'] - 1;
       $all_num = $users['total_users'] - 1;
       $class_adm = ( strpos($views['administrator'], 'current') === false ) ? "" : "current";
       $class_all = ( strpos($views['all'], 'current') === false ) ? "" : "current";
       $views['administrator'] = '<a href="users.php?role=administrator" class="' . $class_adm . '">' . translate_user_role('Administrator') . ' <span class="count">(' . $admins_num . ')</span></a>';
       $views['all'] = '<a href="users.php" class="' . $class_all . '">' . __('All') . ' <span class="count">(' . $all_num . ')</span></a>';
       return $views;
    }
    // END HIDE USER BASED ON USERNAME
    • This topic was modified 4 years, 1 month ago by Chris.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi,

    Try ‘editable_roles’ filter, like below:

    
    add_filter('editable_roles', 'exclude_admin_role');
    
    function exclude_admin_role( $roles ) {
    
        if ( isset( $roles['dailyadmin'] ) ) {
            unset( $roles['dailyadmin'] );
        }
    
        return $roles;
    }
    
    Plugin Author Vladimir Garagulya

    (@shinephp)

    Do not forget to add current user role checking in order do not hide role for the superadmin also.

    Thread Starter Chris

    (@christof15)

    Hi thanks for your response! Very appreciated. How can I check current user role?
    When I use your code the dailyadmin gets hidden for the dailyadmin.

    I changed it now like so:

    add_filter('editable_roles', 'exclude_admin_role');
    
    function exclude_admin_role( $roles ) {
    
        if ( isset( $roles['dailyadmin'] ) ) {
           	unset( $roles['administrator'] );
        	}
    
        return $roles;
    }
    Plugin Author Vladimir Garagulya

    (@shinephp)

    Replace function body with this version:

    
    $user = wp_get_current_user();
    if ( is_a( $user, 'WP_User' ) && in_array( 'administrator', $user->roles ) ) {
         return $roles;
    }
    if ( isset( $roles['administrator'] ) ) {
        unset( $roles['administrator'] );
    }
    
    return $roles;
    
    Thread Starter Chris

    (@christof15)

    Awesome! Thank you so much. That’s exactly what I needed.

    Here’s the final snippet if anyone needs it:

    
    // START Hide Administrator Role
    add_filter('editable_roles', 'exclude_admin_role');
    
    function exclude_admin_role($roles)
    {
    
        $user = wp_get_current_user();
        if (is_a($user, 'WP_User') && in_array('administrator', $user->roles)) {
            return $roles;
        }
        if (isset($roles['administrator'])) {
            unset($roles['administrator']);
        }
    
        return $roles;
    }
    // END Hide Administrator Role
    
    • This reply was modified 4 years, 1 month ago by Chris.
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Hide user role from Dropdown selection’ is closed to new replies.