我希望创建一个自定义功能来访问插件的接口。
插件是否应该在激活时管理将此功能添加到所有管理员帐户
我希望创建一个自定义功能来访问插件的接口。
插件是否应该在激活时管理将此功能添加到所有管理员帐户
<?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 );
}
}
Note: 您可以向角色添加该功能,而无需授予访问权限-只需设置第二个参数$grant = false;
. 这允许只需添加包含最后一个参数为true的cap即可将单个用户列入白名单。对于我目前正在开发的插件,我想在每个角色的基础上授予/限制对插件设置(即相应的管理菜单页面)的访问权限<因此,我不得不add a new plugin-specific capability
to the user roles
.
不幸地kaiser\'s answer 似乎不再工作了,所以我花了一些时间试图找出如何允许上述功能。
在我与您分享我的代码之前,以下是所有内容的简单文本:
插件激活时,添加新功能THE_NEW_CAP
具有某种内置功能的角色BUILT_IN_CAP
(就我而言:edit_pages
).1. (即,再次添加功能)。只有当您想说明插件激活后可能创建的新角色时,才需要这样做。因此,这些新角色没有插件特定的功能,即使它们具有所需的内置功能
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\');
}
}
}
}
注意:请不要使用大写功能。这只是为了可读性This works for me:
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 );
}
}
}
}
关于WP multisite,我有几个问题,希望广泛使用过它的人能够提供帮助:是否可以在多个不同的域上管理多个WP站点</如何管理插件,有些插件我想在所有网站上使用,但有些插件我只想在某些网站上使用。还有,每个插件设置会发生什么变化,它们是否会全面推出</更新-如何对每个站点的插件和WP本身进行管理