你可以用几种方法来实现这一点,这取决于你想得到的具体程度。比如,您是否只需要为一个ID为21的用户禁用,或者这只是一个示例?你是想禁用几个页面还是只禁用几个页面?
<小时>Example 1: You can do this to redirect the dashboard to the edit posts page if not an administrator.
add_action(\'load-index.php\', function(){
if(!current_user_can(\'administrator\')){
if(get_current_screen()->base == \'dashboard\')
wp_redirect(admin_url(\'edit.php\'));
}
});
<小时>
Example 2: Or for a lot of pages you could do something like this:function bw_redirect_admin_pages() {
if(!current_user_can(\'administrator\')){
global $pagenow;
$admin_pages = array(
\'options-writing.php\',
\'options-reading.php\',
\'options-discussion.php\',
\'options-media.php\',
\'options-privacy.php\',
\'options-permalink.php\',
);
if(in_array($pagenow, $admin_pages)){
wp_redirect( admin_url(\'/\') );
exit;
}
}
}
add_action(\'admin_init\', \'bw_redirect_admin_pages\');
旁注:
If you need to be more specific on users you can use wp_get_current_user()
<小时>Example 3: You can also change individual capabilities for other users.
比如说your 用户ID为1。现在您可以使用map_meta_cap
并阻止非您的用户使用某些功能。因此,在下面的示例中,您说如果用户user 1 (或者不管你的ID是什么)他们都不能删除或编辑用户。
add_filter(\'map_meta_cap\', \'prevent_user_edit\', 10, 4 );
function prevent_user_edit( $required_caps, $cap, $user_id, $args ){
$protected_user = 1; // ID of user not editable (this should be YOUR user id)
if ( $user_id === $protected_user ) // Don\'t block caps if current user = protected user
return $required_caps;
$blocked_caps = array(
\'delete_users\',
\'edit_users\'
);
if ( in_array( $cap, $blocked_caps ) && $args[0] === $protected_user )
$required_caps[] = \'do_not_allow\';
return $required_caps;
}
每个管理菜单和子菜单都与不同角色的特定功能相关联。因此,在上面的示例中,我们不允许您以外的所有用户访问用户页面,也不允许他们删除用户
See the Roles vs Capability table to decide what to block as well as the menus associated with those capabilities here.
这比仅仅“重定向页面”要安全得多。上面的例子很好地说明了这一点。如果只在页面加载时将用户从“用户”页面重定向出去,他们仍然可以
delete_users
您不想要的功能。
然后,如果希望重定向,可以将此示例与其中一个重定向示例相结合。
<小时>Example 4: Or another way to do the same thing above but with your email instead of your ID:
add_filter( \'user_has_cap\',
function( $caps, $cap, $args ) {
$user_id = $args[1];
$user = new WP_User( $user_id );
$email = $user->user_email;
if ( $email != get_option(\'admin_email\') )
$caps[\'delete_user\'] = false;
$caps[\'edit_user\'] = false;
return $caps;
}, 10, 3 );
Here are some of the more common capabilities you can block
Example 5: I highly suggest giving 20 people administration roles though. You\'d be much safer creating a custom role:add_action( \'after_switch_theme\', \'create_new_user_role\' );
function create_new_user_role() {
$add_role = add_role( \'manager\', __( \'Site Manager\' ),
array(
\'read\' => true,
\'edit_posts\' => true,
\'edit_pages\' => true,
\'edit_others_posts\' => true,
\'create_posts\' => true,
\'manage_categories\' => true,
\'publish_posts\' => true
));
}
<小时>
Example 6: Change Displayed Role name如果他们一定要扮演“管理员”的角色,你可以耍些花招。但我还没有真正测试过这个。
function brw_change_administrator_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
// Change administrator role name to developer
$wp_roles->roles[\'administrator\'][\'name\'] = \'Developer\';
$wp_roles->role_names[\'administrator\'] = \'Developer\';
// Change editor role name to administrator
$wp_roles->roles[\'administrator\'][\'name\'] = \'Administrator\';
$wp_roles->role_names[\'administrator\'] = \'Administrator\';
}
add_action(\'init\', \'brw_change_administrator_role_name\');
同样,您也可以尝试以下操作(尽管未测试):
add_filter( \'gettext\', \'brw_translate_words_array\' );
add_filter( \'ngettext\', \'brw_translate_words_array\' );
function brw_translate_words_array( $translated ) {
$words = array(
\'Administrator\' => \'Developer\',
\'Editor\' => \'Administrator\'
);
$translated = str_ireplace( array_keys($words), $words, $translated );
return $translated;
}