The code below will completely disable any commenting and trackback features in WordPress. It is in response to the usual issue whereby you think comments have been completely turned-off only to get a notification that there are a new comments waiting moderation.
This code won't turn off any "This post has X comments" or front-end code that displays comments that may be in your theme. Instead it removes the ability for comments to be added/stored against posts. To remove front-end comment boxes and listings you'll need to change your individual templates
Simply copy and paste into your functions.php file in your WordPress theme folder.
<?php # https://keithgreer.uk/wordpress-code-completely-disable-comments-using-functions-php
/* PURPOSE
* Disable comments on ALL post types
* remove/hide any existing comments
* from displaying and hide forms.
*/
/* INSTALL
* Add into your active theme's functions.php file.
*/
/* 1. Disable Comments on ALL post types */
function disable_comments_post_types_support() {
$types = get_post_types();
foreach ($types as $type) {
if(post_type_supports($type, 'comments')) {
remove_post_type_support($type, 'comments');
remove_post_type_support($type, 'trackbacks');
}
}
}
add_action('admin_init', 'disable_comments_post_types_support');
/* 2. Hide any existing comments on front end */
function disable_comments_hide_existing_comments($comments) {
$comments = array();
return $comments;
}
add_filter('comments_array', 'disable_comments_hide_existing_comments', 10, 2);
/* 3. Disable commenting */
function disable_comments_status() {
return false;
}
add_filter('comments_open', 'disable_comments_status', 20, 2);
add_filter('pings_open', 'disable_comments_status', 20, 2);