^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数add_role()用法示例

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

Add role, if it does not exist.


描述


参数

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

更新日志

Version 描述
2.0.0 Introduced.

相关函数

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.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈