2

Find All Users With Specific User Role In WordPress

This is a useful little function that you can use to find users by their user roles. You just call this function in your theme and pass which user role you want to return users from.

Sometimes you want to do this to prevent or allow users with a certain role access to an area of the dashboard, or maybe ability to view/post/edit posts or pages of your site using current_user_can(), which may be combined with the info you get from this function for example. Read about User Roles and Capabilities for more information on this topic.


/**
 * Finds all users with specified role.
 *
 * @param string $role role to find
 * @return array found users
 */
function get_users_with_role( $role ) {
	$wp_user_search = new WP_User_Search( '', '', $role );
	return $wp_user_search->get_results();
}
Levels: Syntax:

User Comments

( ADD YOURS )
  1. Anonymous April 21


    To set edit or manipulate access control for each respective user group role click on the edit icon next to a group name.

  2. Anonymous April 24


    It uses a properties text file to load the users passwords and roles from. Name Description users location of the properties file This file uses the .The format of the properties are as follows each line defining a user its password and the associated roles .user password role role ....jaas config name karaf jaas module className org.apache.karaf.jaas.modules.properties.PropertiesLoginModule flags required users karaf.base etc users.properties jaas module jaas config The OsgiConfigLoginModule uses the OSGi ConfigurationAdmin service to provide the users passwords and roles. The JDBCLoginModule uses a database to load the users passwords and roles from provided a data source normal or XA .

Trackbacks


  1. Avatar

    Your Name
    May 17


    CommentLuv badge