WordPress函数文档add_role()

添加新的用户角色 描述 Adds a new role to WordPress. NB: This sett…

添加新的用户角色

描述

Adds a new role to WordPress.

NB: This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation

用法

 <?php add_role$role$display_name$capabilities ); ?> 

参数

$role

(string) (必填) Role name

默认值: None

$display_name

(string) (必填) Display name for role

默认值: None

$capabilities

(array) (可选) Array of capabilities (see Roles and Capabilities for list of available capabilities)

默认值: array()

注意

When to call

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

mu-plugins will load too early, so use an 动作 hook (like ‘init’) to wrap your add_role() call if you’re doing this in the context of an mu-plugin.

Delete existing role

If you are defining a custom role, and adding capabilities to the role using add_role(), be aware that modifying the capabilities array and re-executing add_role() will not necessarily update the role with the new capabilities list. The add_role() function short-circuits if the role already exists in the database.

The workaround in this case is to precede your add_role() call with a remove_role() call that targets the role you are adding.

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

相关

Roles and Capabilities:

  • add_role()
  • remove_role()
  • get_role()
  • add_cap()
  • remove_cap()
  • 原文:http://codex.wordpress.org/Function_Reference/add_role
类别:WordPress函数文档

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT