首页 问答 正文

在WordPress中如何禁用评论?

注册会员 / 悠然自学 / 2023-06-12/ 浏览 134 次

在WordPress中禁用评论有以下几种方式:

  1. 在WordPress后台设置中禁用评论:

    • 登录WordPress后台。
    • 在左侧导航菜单中选择“设置” -> “讨论”。
    • 在“默认文章设置”部分,取消选中“允许针对新文章的评论。”。
    • 单击“保存更改”按钮即可。
  2. 在特定文章或页面中禁用评论:

    • 在编辑文章或页面时,向下滚动到“讨论”部分。
    • 取消选中“允许评论”复选框。
    • 更新文章或页面。

示例代码:

/*
Plugin Name: Disable Comments
Plugin URI: https://example.com/
Description: Disable comments on the entire site.
Author: Your Name
Version: 1.0
Author URI: https://example.com/
*/

function disable_comments() {
    // Disable support for comments and trackbacks in post types
    $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');

// Remove comments feed
function disable_comments_feed($feeds) {
    if (is_feed()) {
        remove_action('do_feed_rss2', 'do_feed_rss2', 10, 1);
        remove_action('do_feed_atom', 'do_feed_atom', 10, 1);
        remove_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
    }
    return $feeds;
}
add_filter('wp_default_feeds', 'disable_comments_feed', 10, 1);

// Redirect any user trying to access comments page
function disable_comments_redirect() {
    global $pagenow;
    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url()); // Redirects to the dashboard
        exit();
    }
}
add_action('admin_init', 'disable_comments_redirect');

// Remove comments links from admin bar
function disable_comments_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action('wp_before_admin_bar_render', 'disable_comments_admin_bar_render'); 

以上代码是一个简单的插件,需要将代码复制到你的主题文件夹下的disable-comments.php文件中,并将其上传到WordPress的wp-content/plugins/目录中。然后在WordPress后台的“插件”菜单中激活即可禁用评论功能。

大家谈论
    我的见解