Last active
October 22, 2024 18:34
-
-
Save estevan-ulian/434c38761adcf8117dc2243c54830926 to your computer and use it in GitHub Desktop.
Completely disables comments on a Wordpress site.
This file contains hidden or 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 | |
if (!class_exists(WP_Disable_Comments)) { | |
class WP_Disable_Comments | |
{ | |
private static $instance = null; | |
public static function get_instance() | |
{ | |
if (self::$instance === null) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
private function __construct() | |
{ | |
$this->init_hooks(); | |
} | |
private function init_hooks() | |
{ | |
add_action('admin_init', [$this, 'disable_admin_comments']); | |
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); | |
add_action('admin_menu', [$this, 'remove_comments_admin_menu']); | |
add_action('init', [$this, 'remove_admin_bar_comments']); | |
add_filter('rest_endpoints', [$this, 'disable_rest_api_comments']); | |
} | |
public function disable_admin_comments() | |
{ | |
global $pagenow; | |
if ($pagenow === 'edit-comments.php') { | |
wp_safe_redirect(admin_url()); | |
exit; | |
} | |
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); | |
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'); | |
} | |
} | |
} | |
public function remove_comments_admin_menu() | |
{ | |
remove_menu_page('edit-comments.php'); | |
} | |
public function remove_admin_bar_comments() | |
{ | |
if (is_admin_bar_showing()) { | |
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); | |
} | |
} | |
public function disable_rest_api_comments($endpoints) | |
{ | |
if (isset($endpoints['/wp/v2/comments'])) { | |
unset($endpoints['/wp/v2/comments']); | |
} | |
if (isset($endpoints['/wp/v2/comments/(?P<id>[\d]+)'])) { | |
unset($endpoints['/wp/v2/comments/(?P<id>[\d]+)']); | |
} | |
return $endpoints; | |
} | |
} | |
WP_Disable_Comments::get_instance(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment