add_role() WordPress Function

The add_role() function is used to create new roles for users in a WordPress site. This function takes two parameters: the role name and a capabilities array. The role name is a string that is used to identify the role, while the capabilities array is used to specify the capabilities that the role will have.

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

Adds a role, if it does not exist.


Parameters

$role

(string)(Required)Role name.

$display_name

(string)(Required)Display name for role.

$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|null) WP_Role object if role is added, null if already exists.


Top ↑

Source

File: wp-includes/capabilities.php

function add_role( $role, $display_name, $capabilities = array() ) {
	if ( empty( $role ) ) {
		return;
	}
	return wp_roles()->add_role( $role, $display_name, $capabilities );
}


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.