add_role

Add role, if it does not exist.

Description

(WP_Role|null) add_role( (string) $role, (string) $display_name, (array) $capabilities = array() ); 

Returns (WP_Role|null)

WP_Role object if role is added, null if already exists.

Parameters (3)

0. $role (string)
Role name.
1. $display_name (string)
Display name for role.
2. $capabilities — Optional. (array) => array()
List of capabilities, e.g. array( edit_posts => true, delete_posts => false );

Usage

  1. if ( !function_exists( 'add_role' ) ) { 
  2. require_once ABSPATH . WPINC . '/capabilities.php'; 
  3.  
  4. // Role name. 
  5. $role = ''; 
  6.  
  7. // Display name for role. 
  8. $display_name = ''; 
  9.  
  10. // List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false ); 
  11. $capabilities = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = add_role($role, $display_name, $capabilities); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/capabilities.php  
  1. function add_role( $role, $display_name, $capabilities = array() ) { 
  2. if ( empty( $role ) ) { 
  3. return; 
  4. returnwp_roles)->add_role( $role, $display_name, $capabilities );