Support » Fixing WordPress » Exclude Roles from new WP_Roles()

  • Hello, is it possible to exclude roles from new WP_Roles()? Basically I built a dropdown of user roles which works great; however, I need to exclude certain roles such as Administrator and Editor. I haven’t found any examples of this or much documentation at all. Maybe there’s a better way of achieving this I haven’t found?

    
    <select class="author-search-select" name="staff_type" onchange="filterdata.submit();">
    	<option value="all">Show All</option>
    	<?php $roles = new WP_Roles();
    	$roles_names = $roles->get_names();
    	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;
    		} ?>
    	    <option value="<?php echo $role_slug; ?>"<?php echo $set_select_staff_type; ?>><?php echo $role_name; ?></option>
    	<?php } ?>
    </select>
    
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello InHouse,

    Thanks for the question about controlling the roles in WordPress. I am sure there is a solution you can develop that will address it, but there is also at least one plugin that I know of that could help you accomplish: Advanced Access Manager. It works with version 4.7 and provides a lot of functionality including the option to manage, create or delete roles.

    I hope that this is a viable solution for you.

    Regards,
    Arnel C.

    Thanks @arncus but my question is regarding listing users publicly and filtering by role. The dropdown appears at the top of a page. I simply need to list all user roles and exclude a couple.

    Inside the foreach, just check if the role it’s currently up to is one that you’d like to list. If it isn’t, use continue before outputting the option tag.

    • This reply was modified 3 years, 9 months ago by Jacob Peattie.

    Ah ok. I wasn’t sure if new WP_Roles() would accept some kind of $args.

    In case it helps anyone else I’m pasting my working code below.

    
    $roles = new WP_Roles();
    $roles_names = $roles->get_names();
    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 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude Roles from new WP_Roles()’ is closed to new replies.