/**
* Completely disable WordPress comments + harden against comment/pingback spam.
* Safe to run "Everywhere" in Fluent Snippets or Code Snippets.
*/
// --- Admin-facing removal ---------------------------------------------
add_action( 'admin_init', function () {
global $pagenow;
// Redirect users away from the Comments page
if ( $pagenow === 'edit-comments.php' ) {
wp_redirect( admin_url() );
exit;
}
// Remove comment and trackback support from all post types
foreach ( get_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' );
}
}
} );
// Remove the Recent Comments dashboard widget
add_action( 'wp_dashboard_setup', function () {
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
} );
// Remove the Comments menu from the admin sidebar
add_action( 'admin_menu', function () {
remove_menu_page( 'edit-comments.php' );
} );
// Remove the Comments icon from the admin bar
add_action( 'wp_before_admin_bar_render', function () {
global $wp_admin_bar;
if ( is_object( $wp_admin_bar ) ) {
$wp_admin_bar->remove_menu( 'comments' );
}
} );
// --- Front-end / API hardening (the part that actually stops spam) ----
// Close comments and pings everywhere
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 );
// Block direct POSTs to wp-comments-post.php
add_action( 'pre_comment_on_post', function () {
wp_die(
__( 'Comments are disabled on this site.' ),
'',
array( 'response' => 403 )
);
} );
// Remove the comments endpoints from the REST API
add_filter( 'rest_endpoints', function ( $endpoints ) {
unset(
$endpoints['/wp/v2/comments'],
$endpoints['/wp/v2/comments/(?P[\d]+)']
);
return $endpoints;
} );
// Disable XML-RPC pingbacks and the X-Pingback header
add_filter( 'wp_headers', function ( $headers ) {
unset( $headers['X-Pingback'] );
return $headers;
} );
add_filter( 'xmlrpc_methods', function ( $methods ) {
unset(
$methods['pingback.ping'],
$methods['pingback.extensions.getPingbacks']
);
return $methods;
} );