在WordPress中禁用评论有以下几种方式:
在WordPress后台设置中禁用评论:
在特定文章或页面中禁用评论:
示例代码:
/*
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后台的“插件”菜单中激活即可禁用评论功能。