在某些情况下,网站管理员可能希望完全禁用WordPress的评能,以避免垃圾评论或专注于内容展示。本文将详细介绍几种有效的禁用方法。
进入WordPress后台,依次点击"设置"→"讨论",取消勾选"允许他人在新文章上发表评论"选项,并保存更改。
在主题的functions.php文件中添加以下代码:
// 禁用所有文章的评能
function disable_comments() {
// 禁用现有评论
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// 隐藏现有评论
add_filter('comments_array', '__return_empty_array', 10, 2);
// 移除评能支持
remove_post_type_support('post', 'comments');
remove_post_type_support('page', 'comments');
}
add_action('init', 'disable_comments');
在functions.php中添加以下代码移除后台评论菜单:
function remove_comments_menu() {
remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'remove_comments_menu');
1. 在进行任何代码修改前,请务必备份网站数据
2. 建议使用子主题进行修改,避免主题更新导致代码丢失
3. 禁用评论后,现有的评论数据仍会保留在数据库中
通过以上方法,您可以有效地禁用WordPress的评能,让网站更加专注于内容展示,同时避免垃圾评论的困扰。