首页 问答 正文

如何在wordpress中禁用评论功能?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 165 次

在WordPress中禁用评论功能,可以通过以下两种方法实现:

方法一:在后台设置中禁用评论

  1. 登录WordPress后台,选择“设置” -> “讨论”;
  2. 在“默认文章设置”中,取消勾选“允许将对此文章的评论”和“允许将新评论通知我”,如下图所示;
  3. 点击“保存更改”按钮,即可禁用评论功能。

禁用评论功能

方法二:通过代码禁用评论

  1. 登录WordPress后台,选择“外观” -> “编辑器”;
  2. 打开当前使用的主题的functions.php文件;
  3. 在文件末尾加入以下代码:
function disable_comments_post_types_support() {
    $post_types = get_post_types();
    foreach ($post_types as $post_type) {
        if(post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
}
add_action('admin_init', 'disable_comments_post_types_support');

// 关闭评论
function disable_comments_status() {
    return false;
}
add_filter('comments_open', 'disable_comments_status', 20, 2);
add_filter('pings_open', 'disable_comments_status', 20, 2);

// 隐藏评论菜单
function disable_comments_admin_menu() {
    remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'disable_comments_admin_menu');

// 隐藏评论链接
function disable_comments_admin_bar() {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
}
add_action('init', 'disable_comments_admin_bar'); 
  1. 点击“更新文件”按钮,即可禁止评论功能。

以上是禁用评论的方法,将以上代码添加到functions.php中即可禁用评论功能。

注意:禁用评论功能后,己有的文章评论仍然会显示,您需要手动关闭每一篇文章的评论功能,或者使用以下的SQL语句将所有文章的评论功能关闭:

UPDATE wp_posts SET comment_status = 'closed'; 
大家谈论
    我的见解