纯代码让wordpress作者不能看到后面台的个人资料页

纯代码让wordpress作者不能看到后面台的个人资料页

可以通过在WordPress主题的functions.php文件中添加代码来限制作者访问后台个人资料页。以下是实现方法:

// 禁止作者角色访问个人资料页
function restrict_author_profile_access() {
    if (current_user_can('author') && strpos($_SERVER['REQUEST_URI'], 'profile.php') !== false) {
        wp_redirect(home_url());
        exit;
    }
}
add_action('admin_init', 'restrict_author_profile_access');

// 隐藏个人资料菜单项
function remove_author_profile_menu() {
    if (current_user_can('author')) {
        remove_menu_page('profile.php');
    }
}
add_action('admin_menu', 'remove_author_profile_menu');

这段代码会做两件事:

1、当作者尝试访问个人资料页时自动重定向到首页;

2、从后台菜单中移除个人资料链接。

代码需要添加到当前主题的functions.php文件末尾,添加前建议备份文件