Created
April 17, 2023 08:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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