Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithgreer/0e30df9ab8b05898455ccdf35a07e48b to your computer and use it in GitHub Desktop.
Save keithgreer/0e30df9ab8b05898455ccdf35a07e48b to your computer and use it in GitHub Desktop.
How to completely disable comments in WordPress using functions.php- https://keithgreer.dev/wordpress-code-completely-disable-comments-using-functions-php
<?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 updated_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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment