返回介绍

add_role()

发布于 2017-09-10 21:19:02 字数 4123 浏览 1556 评论 0 收藏 0

add_role( string $role,  string $display_name,  array $capabilities = array() )

Add role, if it does not exist.


description


参数

$role

(string) (Required) Role name.

$display_name

(string) (Required) Display name for role.

$capabilities

(array) (Optional) List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );

Default value: array()


返回值

(WP_Role|null) WP_Role object if role is added, null if already exists.


源代码

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 );
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/capabilities.php: wp_roles()
  • wp-includes/class-wp-roles.php: WP_Roles::add_role()

Used By

  • wp-admin/includes/schema.php: populate_roles_160()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Usage

    <?php add_role( $role, $display_name, $capabilities ); ?>
  2. Example
    Create a new “Guest Author” role.

    $result = add_role(
    	'guest_author',
    	__( 'Guest Author', 'testdomain' ),
        array(
    		'read'         => true,  // true allows this capability
    		'edit_posts'   => true,
    		'delete_posts' => false, // Use false to explicitly deny
        )
    );
    
    if ( null !== $result ) {
        echo "Success: {$result->name} user role created.";
    }
    else {
        echo 'Failure: user role already exists.';
    }

    Create a new role when a plugin is activated
    See register_activation_hook.

    function add_roles_on_plugin_activation() {
           add_role( 'custom_role', 'Custom Subscriber', array( 'read' => true, 'level_0' => true ) );
       }
    register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );

    Note: When to call
    Make sure the global $wp_roles is available before attempting to add or modify a role. The best practice is to use a plugin (or theme) activation hook to make changes to roles (since you only want to do it once!).

    mu-plugins loads too early, so use an action hook (like 'init') to wrap your add_role() call if you’re doing this in the context of an mu-plugin.

    Note: Delete existing role
    You can not change the capabilities of an existing role using add_role(). This function will stop executing and return null is the specified role name already exists.

    You can change a user role’s capabilities (or display name) by using remove_role(), then add_role().

    This is for development only. Once you have nailed down your list of capabilities, there’s no need to keep the remove_role() code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文