首页 问答 正文

如何禁用WordPress中的评论功能?

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

要禁用WordPress中的评论功能,可以采取以下步骤:

  1. 登录WordPress管理后台
  2. 在侧边栏中选择“设置”->“讨论”
  3. 将“允许读者在文章下发表评论”选项取消勾选
  4. 点击“保存更改”按钮

如果你需要在特定页面或文章中禁用评论功能,可以在撰写文章时,在右侧的“讨论”区域,取消勾选“允许评论”选项即可。

如果上述方法无法实现需求,也可以通过代码的方式禁用评论功能。具体方法如下:

在你的主题中的functions.php文件中添加以下代码:

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_hide_existing_comments($comments) {
    $comments = array();
    return $comments;
}
add_filter('comments_array', 'disable_comments_hide_existing_comments', 10, 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'); 

这段代码会移除所有可能存在的评论相关功能和选项。具体来说,它会:

  • 从所有文章、页面、附件和自定义文章类型中删除评论和引用的支持。
  • 在WordPress管理菜单中移除评论菜单。
  • 在WordPress工具栏中移除评论图标。
  • 隐藏现有的评论。
  • 禁用评论和引用。

注意:在添加代码前,请先备份您的functions.php文件。如果您不确定如何编辑主题函数文件,请咨询专业人士。

返回格式

要禁用WordPress中的评论功能,可以采取以下步骤:
1. 登录WordPress管理后台
2. 在侧边栏中选择“设置”->“讨论”
3. 将“允许读者在文章下发表评论”选项取消勾选
4. 点击“保存更改”按钮

如果你需要在特定页面或文章中禁用评论功能,可以在撰写文章时,在右侧的“讨论”区域,取消勾选“允许评论”选项即可。

如果上述方法无法实现需求,也可以通过代码的方式禁用评论功能。具体方法请见上文。

注意:在添加代码前,请先备份您的functions.php文件。如果您不确定如何编辑主题函数文件,请咨询专业人士。 
大家谈论
    我的见解