类文档

WP_Role

💡 云策文档标注

概述

WP_Role 是 WordPress 核心类,用于扩展用户角色 API,提供角色管理和能力(capability)操作功能。它允许开发者创建、修改和检查角色的权限。

关键要点

  • WP_Role 类用于表示用户角色,包含角色名称和能力数组属性。
  • 提供构造函数 __construct 初始化角色名称和能力。
  • 方法 add_cap 用于为角色添加能力,remove_cap 用于移除能力。
  • 方法 has_cap 检查角色是否拥有特定能力,支持通过 role_has_cap 过滤器自定义。

代码示例

$result = add_role(
    'Role Slug', // Ex. customer_role
    __( 'Role Label' ), // Customer Role
    array(
        'read'         => true,  // true allows this capability
        'edit_posts'   => true,
        'delete_posts' => false, // Use false to explicitly deny
    )
);

if ( null !== $result ) {
    echo 'New Role Created!';
}
else {
    echo 'Opps!! the Role already exists.';
}

📄 原文内容

Core class used to extend the user roles API.

Methods

Name Description
WP_Role::__construct Constructor – Set up object properties.
WP_Role::add_cap Assign role a capability.
WP_Role::has_cap Determines whether the role has the given capability.
WP_Role::remove_cap Removes a capability from a role.

Source

class WP_Role {
	/**
	 * Role name.
	 *
	 * @since 2.0.0
	 * @var string
	 */
	public $name;

	/**
	 * List of capabilities the role contains.
	 *
	 * @since 2.0.0
	 * @var bool[] Array of key/value pairs where keys represent a capability name and boolean values
	 *             represent whether the role has that capability.
	 */
	public $capabilities;

	/**
	 * Constructor - Set up object properties.
	 *
	 * The list of capabilities must have the key as the name of the capability
	 * and the value a boolean of whether it is granted to the role.
	 *
	 * @since 2.0.0
	 *
	 * @param string $role         Role name.
	 * @param bool[] $capabilities Array of key/value pairs where keys represent a capability name and boolean values
	 *                             represent whether the role has that capability.
	 */
	public function __construct( $role, $capabilities ) {
		$this->name         = $role;
		$this->capabilities = $capabilities;
	}

	/**
	 * Assign role a capability.
	 *
	 * @since 2.0.0
	 *
	 * @param string $cap   Capability name.
	 * @param bool   $grant Whether role has capability privilege.
	 */
	public function add_cap( $cap, $grant = true ) {
		$this->capabilities[ $cap ] = $grant;
		wp_roles()->add_cap( $this->name, $cap, $grant );
	}

	/**
	 * Removes a capability from a role.
	 *
	 * @since 2.0.0
	 *
	 * @param string $cap Capability name.
	 */
	public function remove_cap( $cap ) {
		unset( $this->capabilities[ $cap ] );
		wp_roles()->remove_cap( $this->name, $cap );
	}

	/**
	 * Determines whether the role has the given capability.
	 *
	 * @since 2.0.0
	 *
	 * @param string $cap Capability name.
	 * @return bool Whether the role has the given capability.
	 */
	public function has_cap( $cap ) {
		/**
		 * Filters which capabilities a role has.
		 *
		 * @since 2.0.0
		 *
		 * @param bool[] $capabilities Array of key/value pairs where keys represent a capability name and boolean values
		 *                             represent whether the role has that capability.
		 * @param string $cap          Capability name.
		 * @param string $name         Role name.
		 */
		$capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );

		if ( ! empty( $capabilities[ $cap ] ) ) {
			return $capabilities[ $cap ];
		} else {
			return false;
		}
	}
}

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Creating the New Role

    $result = add_role(
        'Role Slug', // Ex. customer_role
        __( 'Role Label' ), // Customer Role
        array(
            'read'         => true,  // true allows this capability
            'edit_posts'   => true,
            'delete_posts' => false, // Use false to explicitly deny
        )
    );
    
    if ( null !== $result ) {
        echo 'New Role Created!';
    }
    else {
        echo 'Opps!! the Role already exists.';
    }