Answers:
首先,请确保在激活时添加的所有内容在卸载时也都将被删除。我得到了一个简短的教程,其中包括示例代码。
我对MU的了解并不多,但是据我所知,角色对象在所有博客中都是全局的。只需尝试这个小插件,看看您能得到什么:
<?php
/*
Plugin Name: MU Roles check
Plugin URI: https://github.com/franz-josef-kaiser/
Description: Check roles during viewing a blog
Author: Franz Josef Kaiser
Author URI: https://plus.google.com/u/0/107110219316412982437
Version: 0.1
Text Domain: murc
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/**
* Show the blog data and the role names in this blog
* Also shows if the custom capability was successfully added, or displays n/a for the role
*
* @return void
*/
function wpse35165_role_check()
{
$blog = get_current_site();
$custom_cap = 'name_of_your_custom_capability';
$html = "<hr /><table>";
$html .= "<caption>List roles in (Blog) {$blog->site_name} / ID#{$blog->id}</caption>"
$html .= "<thead><tr><th>Role Name</th><th>Capabilties</th></tr></thead><tbody>";
foreach ( $GLOBALS['wp_roles'] as $name => $role_obj )
{
$cap = in_array( $custom_cap, $role_obj->caps ) ? $custom_cap : 'n/a';
$cap = $cap OR in_array( $custom_cap, $role_obj->allcaps ) ? $custom_cap : 'n/a';
$html .= "<tr><td>{$name}</td><td>{$cap}</td></tr>";
}
$html .= '</tbody></table>';
print $html;
}
add_action( 'shutdown', 'wpse35165_role_check' );
/**
* Add the capability to the role objects
* Should be in your activation function and done before you inspect with your plugin
*
* @return void
*/
function wpse35165_add_cap()
{
$custom_cap = 'name_of_your_custom_capability';
$min_cap = 'the_minimum_required_built_in_cap'; // Check "Roles and objects table in codex!
$grant = true;
foreach ( $GLOBALS['wp_roles'] as $role_obj )
{
if (
! $role_obj->has_cap( $custom_cap )
AND $role_obj->has_cap( $min_cap )
)
$role_obj->add_cap( $custom_cap, $grant );
}
}
注意:您可以在不授予角色访问权限的情况下将其添加到角色中,只需设置第二个参数即可$grant = false;
。这样一来,只要将包含最后一个参数的上限添加为true,即可将单个用户列入白名单。
对于我当前正在使用的插件,我想基于每个角色授予/限制对插件设置(即相应的管理菜单页面)的访问。
因此,我必须向添加新的特定capability
于插件的插件user roles
。
不幸的是,kaiser的答案似乎不再起作用,因此我花了一些时间试图弄清楚如何允许上述功能。
在我与您分享代码之前,以下是所有内容,以纯文本形式显示:
THE_NEW_CAP
到具有某种内置功能的角色中BUILT_IN_CAP
(在我的情况下:)edit_pages
。这是上面的列表转换为代码:
class WPSE35165Plugin {
public function __construct() {
// Register hooks
register_activation_hook(__FILE__, array(__CLASS__, 'activation'));
register_deactivation_hook(__FILE__, array(__CLASS__, 'deactivation'));
// Add actions
add_action('admin_menu', array(__CLASS__, 'admin_menu'));
}
public function activation() {
self::add_cap();
}
// Add the new capability to all roles having a certain built-in capability
private static function add_cap() {
$roles = get_editable_roles();
foreach ($GLOBALS['wp_roles']->role_objects as $key => $role) {
if (isset($roles[$key]) && $role->has_cap('BUILT_IN_CAP')) {
$role->add_cap('THE_NEW_CAP');
}
}
}
// Add plugin menu pages to admin menu
public function admin_menu() {
// Remove the following line if you don't care about new roles
// that have been created after plugin activation
self::add_cap();
// Set up the plugin admin menu
add_menu_page('Menu', 'Menu', 'THE_NEW_CAP', …);
add_submenu_page('wpse35165', 'Submenu', 'Submenu', 'THE_NEW_CAP', ...);
}
public function deactivation() {
self::remove_cap();
}
// Remove the plugin-specific custom capability
private static function remove_cap() {
$roles = get_editable_roles();
foreach ($GLOBALS['wp_roles']->role_objects as $key => $role) {
if (isset($roles[$key]) && $role->has_cap('THE_NEW_CAP')) {
$role->remove_cap('THE_NEW_CAP');
}
}
}
}
注意:请勿使用大写功能。这只是为了提高可读性。
get_editable_roles()
获取要编辑的角色。否则,您将破坏插件。
这对我有用:
add_action('admin_init', 'add_custom_cap');
function add_custom_cap()
{
$custom_cap = 'test_cap';
$min_cap = 'read';
$grant = true;
$to_role = 'your_user_role';
$role = 'user_role';
foreach ( $GLOBALS['wp_roles'] as $role_obj )
{
if (is_object($role_obj[$role])) {
if (!$role_obj[$role]->has_cap( $custom_cap ) && $role_obj[$role]->has_cap( $min_cap )) {
$role_obj[$role]->add_cap( $custom_cap, $grant );
}
}
}
}