WP_Roles::add_role() WordPress Method

The WP_Roles::add_role() method is used to add a role to the list of available roles for the site. This role can then be assigned to one or more users, allowing them to perform the tasks associated with that role.

WP_Roles::add_role( string $role, string $display_name, bool[] $capabilities = array() ) #

Add role name with capabilities to list.


Description

Updates the list of roles, if the role doesn’t already exist.

The capabilities are defined in the following format array( 'read' => true ); To explicitly deny a role a capability you set the value for that capability to false.


Top ↑

Parameters

$role

(string)(Required)Role name.

$display_name

(string)(Required)Role display name.

$capabilities

(bool[])(Optional)List of capabilities keyed by the capability name, e.g. array( 'edit_posts' => true, 'delete_posts' => false ).

Default value: array()


Top ↑

Return

(WP_Role|void) WP_Role object, if role is added.


Top ↑

Source

File: wp-includes/class-wp-roles.php

	public function add_role( $role, $display_name, $capabilities = array() ) {
		if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
			return;
		}

		$this->roles[ $role ] = array(
			'name'         => $display_name,
			'capabilities' => $capabilities,
		);
		if ( $this->use_db ) {
			update_option( $this->role_key, $this->roles );
		}
		$this->role_objects[ $role ] = new WP_Role( $role, $capabilities );
		$this->role_names[ $role ]   = $display_name;
		return $this->role_objects[ $role ];
	}


Top ↑

Changelog

Changelog
VersionDescription
2.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.