WordPress 1.6 introduced role-based user management. wp_roles global variable stores the different roles WordPress user can have. It is an instance of class WP_Roles defined in [wordpress root folder]/wp-includes/capabilities.php and is set in [wordpress root folder]/wp-settings.php.

Ryan Boren has a detailed post regarding this. WP_Roles provides following methods:

  • WP_Roles(): the constructor
  • add_role($role, $display_name, $capabilities=''): adds specified role
  • remove_role($role): removes specified role
  • add_cap($role, $cap, $grant = true): adds specified capability to the specified role
  • remove_cap($role, $cap): removes specified capability from the specified role
  • get_role($role): returns specified role
  • get_names(): returns list of names of all the roles
  • is_role($role): queries if the specified role exists or not

Plugin authors can work with following wrapper functions that operate on the global variable for roles and capabilities management:

  • get_role($role)
  • add_role($role, $display_name, $capabilities='')
  • remove_role($role)

$role can be any of the capabilities provided by default (check here), e.g., it can be ‘administrator’, ‘contributor’, …

To manage capabilities of a role, following methods of class WP_Role can be used after acquiring a role by using get_role($role):

  • WP_Role($role, $capabilities)
  • add_cap($cap, $grant = true): adds capabilities
  • remove_cap($cap, $grant = true): removes capabilities
  • has_cap($cap): queries for a specific capability

WP_User can be use to manage a specific user’s roles and capabilities. $current_user_can($capability) can be used to query the current user’s capabilities.

Back to full list of global variables.

Technorati tags: , , ,

Discussion [Participate or Link]

  1. Abhijit Nadgouda @ iface » Blog Archive » allowedtags said:

    […] Optionally, unfiltered_html capability can be added to disable the default filtering of HTML elements specified in [wordpress root folder]/wp-includes/kses.php. Plugins can be written to add custom functions for the filter init to allow your set of HTML elements. unfiltered_html setting should be carefully enabled as it can allow posting of malicious code. […]

  2. Abhijit Nadgouda @ iface » Blog Archive » Wordpress Global Variables said:

    […] $wp_roles […]

  3. Abhijit Nadgouda’s List of WordPress Global Variables « Lorelle on WordPress said:

    […] User Data: wp_roles, user_pass_md5, user_url, user_login, user_level, user_identity, user_email, user_ID, current_user, and wpdb […]

  4. references» Blog Archive » Wordpress Global Variables said:

    […] $wp_roles […]

Say your thought!

If you want to use HTML you can use these tags: <a>, <em>, <strong>, <abbr>, <code>, <blockquote>. Closing the tags will be appreciated as this site uses valid XHTML.



Abhijit Nadgouda
iface Consulting
+91 9819820312
My bookmarks


This is the weblog of Abhijit Nadgouda where he writes down his thoughts on software development and related topics. You are invited to subscribe to the feed to stay updated or check out more subscription options. Or you can choose to browse by one of the topics.