You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何针对WordPress特定用户角色隐藏WP-ADMIN的文章、媒体及评论菜单

实现方案

一、针对非管理员隐藏指定后台菜单

通过判断用户权限,仅对作者角色(非管理员)隐藏文章、媒体、评论菜单,管理员不受影响。将以下代码添加到主题的functions.php文件中:

function remove_non_admin_menus() {
    // 仅对无管理员权限的用户执行(作者角色无manage_options权限)
    if ( !current_user_can( 'manage_options' ) ) {
        // 隐藏文章菜单
        remove_menu_page( 'edit.php' );
        // 隐藏媒体菜单
        remove_menu_page( 'upload.php' );
        // 隐藏评论菜单
        remove_menu_page( 'edit-comments.php' );
    }
}
add_action( 'admin_menu', 'remove_non_admin_menus' );

二、限制作者仅能选择指定分类提交文章

假设你要允许作者使用的分类ID为5和8(请替换为你实际的分类ID),通过过滤分类选择框的参数,仅显示指定分类。添加以下代码到functions.php:

function restrict_author_categories( $args ) {
    // 仅针对作者角色生效
    if ( current_user_can( 'author' ) && !current_user_can( 'manage_options' ) ) {
        // 设置仅显示指定分类ID,替换为你的目标分类ID
        $args['include'] = array( 5, 8 );
        // 隐藏"全部分类"选项(可选)
        $args['show_option_all'] = '';
    }
    return $args;
}
// 过滤文章编辑页的分类下拉框
add_filter( 'wp_dropdown_categories_args', 'restrict_author_categories' );
// 过滤文章编辑页的分类复选框列表
add_filter( 'wp_terms_checklist_args', 'restrict_author_categories' );

补充说明

  • 分类ID可以在WordPress后台的文章 > 分类页面,点击对应分类后从URL中获取(例如taxonomy=category&tag_ID=5中的5就是分类ID)。
  • 如果需要同时限制作者在文章列表页的分类筛选功能,可以额外添加以下代码:
function restrict_author_category_filter( $query ) {
    global $pagenow;
    // 仅在文章列表页且当前用户为作者时生效
    if ( is_admin() && $pagenow == 'edit.php' && current_user_can( 'author' ) && !current_user_can( 'manage_options' ) ) {
        // 强制筛选指定分类的文章
        $query->set( 'cat', '5,8' );
    }
    return $query;
}
add_action( 'pre_get_posts', 'restrict_author_category_filter' );

内容的提问来源于stack exchange,提问作者Scott Davies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 17:10:33