Support » Fixing WordPress » Check if a user role is empty/unused

  • Hello, is it possible to determine if a user role is empty? I’m using WP_Roles to output a list of users but I’d like to hide empty roles. I haven’t seen any documentation that indicates WP_Roles can handle $args so I’m not sure how I’d query to determine if the role is empty/unused. It occurred to me that the roles list on the users page (/wp-admin/users.php) excludes roles if they’re empty/unused but I can’t see how they’re doing that exactly.

    Any point in the right direction is appreciated!

    
    $roles = new WP_Roles();
    $roles_names = $roles->get_names();
    uksort($roles_names, 'strcasecmp');
    foreach ( $roles_names  as $role_slug => $role_name ) {
    	// Check if the staff type is selected in the URL
    	if ( $role_slug == $selected_staff_type ) {
    		$set_select_staff_type = ' selected="selected"';
    	} else {
    		$set_select_staff_type = NULL;
    	}
    	if ( $role_slug !== 'administrator' && $role_slug !== 'editor' && $role_slug !== 'no_role' ) { ?>
    	    <option value="<?php echo $role_slug; ?>"<?php echo $set_select_staff_type; ?>><?php echo $role_name; ?></option>
        <?php }
    }
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Check if a user role is empty/unused’ is closed to new replies.