Title: WP_Role::add_cap
Published: April 25, 2014
Last modified: April 28, 2025

---

# WP_Role::add_cap( string $cap, bool $grant = true )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#wp--skip-link--target)

Assign role a capability.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#parameters)󠁿

 `$cap`stringrequired

Capability name.

`$grant`booloptional

Whether role has capability privilege.

Default:`true`

## 󠀁[More Information](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#more-information)󠁿

Changing the capabilities of a role is persistent, meaning the added capability 
will stay in effect until explicitly revoked.

This setting is saved to the database (in table wp_options, field wp_user_roles),
so it might be better to run this on theme/plugin activation.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#source)󠁿

    ```php
    public function add_cap( $cap, $grant = true ) {
    	$this->capabilities[ $cap ] = $grant;
    	wp_roles()->add_cap( $this->name, $cap, $grant );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-role.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-role.php#L59)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-role.php#L59-L62)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_roles()](https://developer.wordpress.org/reference/functions/wp_roles/)`wp-includes/capabilities.php` |

Retrieves the global [WP_Roles](https://developer.wordpress.org/reference/classes/wp_roles/) instance and instantiates it if necessary.

  | 
| [WP_Roles::add_cap()](https://developer.wordpress.org/reference/classes/wp_roles/add_cap/)`wp-includes/class-wp-roles.php` |

Adds a capability to role.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#comment-content-578)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/classes/wp_role/add_cap/#comment-578)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_role%2Fadd_cap%2F%23comment-578)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_role%2Fadd_cap%2F%23comment-578)
 4.  **Example**
 5.      ```php
         function add_theme_caps() {
         	// gets the author role
         	$role = get_role( 'author' );
     
         	// This only works, because it accesses the class instance.
         	// would allow the author to edit others' posts for current theme only
         	$role->add_cap( 'edit_others_posts' ); 
         }
         add_action( 'admin_init', 'add_theme_caps');
         ```
     
 6.  **NB: This setting is saved to the database, so it might be better to run this
     on theme/plugin activation**
 7.      ```php
         function add_theme_caps(){
         	 global $pagenow;
     
         	 if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){ // Test if theme is active
         		 // Theme is active
         		 // gets the author role
         		 $role = get_role( 'author' );
     
         		 // This only works, because it accesses the class instance.
         		 // would allow the author to edit others' posts for current theme only
         		 $role->add_cap( 'edit_others_posts' ); 
         	 } else {
         		 // Theme is deactivated
         		 // Remove the capacity when theme is deactivate
         		 $role->remove_cap( 'edit_others_posts' ); 
         	 }
         }
         add_action( 'load-themes.php', 'add_theme_caps' );
         ```
     
 8.  To add capability to specific user :
 9.      ```php
         $user = new WP_User( $user_id );
         $user->add_cap( 'can_edit_posts' );
         ```
     
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_role%2Fadd_cap%2F%3Freplytocom%3D578%23feedback-editor-578)
 11.  [Skip to note 4 content](https://developer.wordpress.org/reference/classes/wp_role/add_cap/?output_format=md#comment-content-3524)
 12.   [Will Skora](https://profiles.wordpress.org/skorasaurus/)  [  6 years ago  ](https://developer.wordpress.org/reference/classes/wp_role/add_cap/#comment-3524)
 13. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_role%2Fadd_cap%2F%23comment-3524)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_role%2Fadd_cap%2F%23comment-3524)
 14. Note that add_cap only accept one string; you cannot add multiple using a single
     add_cap call; you need to make multiple add_cap calls like the following:
 15. `
      $your_var -> add_cap( 'edit_others_posts' ); $your_var -> add_cap( 'upload_files');
 16.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_role%2Fadd_cap%2F%3Freplytocom%3D3524%23feedback-editor-3524)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_role%2Fadd_cap%2F)
before being able to contribute a note or feedback.