在WordPress后臺移除“評論”菜單,可以通過以下幾種方法實現。以下是詳細步驟:
方法1:通過代碼移除(推薦)
將以下代碼添加到主題的functions.php文件中(或使用CodeSnippets插件):
// 移除后臺左側菜單的“評論”
add_action('admin_menu', 'remove_comments_menu');
function remove_comments_menu() {remove_menu_page('edit-comments.php'); // 移除頂級菜單
}// 可選:同時移除工具欄(頂部管理條)中的評論鏈接
add_action('admin_bar_menu', 'remove_comments_from_admin_bar', 999);
function remove_comments_from_admin_bar($wp_admin_bar) {$wp_admin_bar->remove_node('comments');
}
方法2:通過角色權限控制(隱藏評論)
如果希望僅對特定用戶角色隱藏評論(如作者、編輯),可以限制其權限:
// 禁止特定角色訪問評論功能
add_action('init', 'disable_comments_for_roles');
function disable_comments_for_roles() {$role = get_role('author'); // 替換為目標角色if ($role) {$role->remove_cap('moderate_comments');$role->remove_cap('edit_comment');}
}
方法3:使用插件(簡單但可能冗余)
安裝插件如”DisableComments”,但注意這會全局禁用評論功能(包括前端),可能不符合僅需隱藏菜單的需求。
注意事項
備份代碼:修改functions.php前先備份。
子主題:如果使用第三方主題,建議通過子主題或插件(如CodeSnippets)添加代碼,避免更新覆蓋。
權限問題:管理員(Administrator)默認仍可訪問,如需完全禁用需額外調整權限。
效果驗證
登錄后臺后,左側菜單的“評論”選項應消失。
直接訪問wodepress.com/wp-admin/edit-comments.php會提示權限不足(非管理員)。
原文
https://www.jianzhanpress.com/?p=8927