Last Updated on January 22, 2025 by Mike Kipruto
Say goodbye to unwanted comments! Ever wondered how to create your own WordPress plugin that completely disables comments with just a few clicks? You’re in the right place! 🎯
In this step-by-step guide, I’ll show you how to build a powerful custom plugin using PHP that not only disables comments site-wide but also removes all related UI elements. And the best part? You’ll be able to toggle these settings directly from your WordPress Admin Dashboard as shown:

So, roll up your sleeves—let’s dive into building a plugin that puts you in full control! 🚀
Step 1: Create Your Plugin File
- Navigate to your WordPress installation’s
wp-content/plugins/
directory. - Create a new folder called
disable-comments-control
. - Inside this folder, create a new file called
disable-comments-control.php
.
Then the following plugin header at the top of the file:
<?php
/**
* Plugin Name: Disable Comments Control
* Description: A plugin to disable comments and trackbacks with admin settings.
* Version: 1.0
* Author: Your Name
*/
Step 2: Define the Plugin Class
We’re going to create a class called DisableCommentsControl
. This class will manage everything: disabling comments, removing related UI elements, and adding a settings page.
Here’s the complete class:
class DisableCommentsControl {
private $settings_option = 'disable_comments_settings';
public function __construct() {
// Hook initialization methods
add_action('admin_init', [$this, 'disable_comments_post_types_support']);
add_filter('comments_open', [$this, 'disable_comments_status'], 20, 2);
add_filter('pings_open', [$this, 'disable_comments_status'], 20, 2);
add_filter('comments_array', [$this, 'disable_existing_comments'], 10, 2);
add_action('admin_menu', [$this, 'admin_menu_setup']);
add_action('admin_init', [$this, 'redirect_comments_page']);
add_action('admin_init', [$this, 'disable_comments_dashboard']);
// Add settings page
add_action('admin_menu', [$this, 'add_settings_page']);
add_action('admin_init', [$this, 'register_settings']);
}
// Other methods are defined below...
}
Step 3: Disable Comments Across Your Site
Here’s what each method in the class does to disable comments:
Remove Comment Support from Post Types
This method ensures that comments and trackbacks are disabled for all public post types (including custom post types):
public function disable_comments_post_types_support() {
if ($this->is_disabled()) {
$post_types = get_post_types(['public' => true], 'names');
foreach ($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');
}
}
}
}
Disable Comments for New and Existing Content
To ensure no new comments are allowed and hide existing ones, we hook into several filters:
public function disable_comments_status() {
return $this->is_disabled() ? false : true;
}
public function disable_existing_comments($comments) {
return $this->is_disabled() ? [] : $comments;
}
Remove the Comments Admin Menu and Redirect
This removes the “Comments” link from the admin sidebar and redirects users who try to visit the comments page:
public function admin_menu_setup() {
if ($this->is_disabled()) {
remove_menu_page('edit-comments.php');
}
}
public function redirect_comments_page() {
if ($this->is_disabled()) {
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
}
}
Step 4: Add a Settings Page
Next, we’ll add a settings page to allow site admins to enable or disable this functionality.
Create the Settings Page
Here’s how we add the page under Settings > Disable Comments:
public function add_settings_page() {
add_options_page(
'Disable Comments Settings',
'Disable Comments',
'manage_options',
'disable-comments-settings',
[$this, 'render_settings_page']
);
}
The settings page looks like this:
public function render_settings_page() {
?>
<div class="wrap">
<h1>Disable Comments Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields($this->settings_option);
do_settings_sections($this->settings_option);
submit_button();
?>
</form>
</div>
<?php
}
Register the Setting
Here, we register a single setting (disable_comments_enabled
) that determines whether comments are disabled:
public function register_settings() {
register_setting($this->settings_option, $this->settings_option);
add_settings_section('disable_comments_main', 'Main Settings', null, $this->settings_option);
add_settings_field(
'disable_comments_enabled',
'Disable Comments and Trackbacks',
[$this, 'render_settings_field'],
$this->settings_option,
'disable_comments_main'
);
}
public function render_settings_field() {
$options = get_option($this->settings_option, ['enabled' => false]);
$enabled = isset($options['enabled']) ? $options['enabled'] : false;
?>
<input type="checkbox" name="<?= esc_attr($this->settings_option) ?>[enabled]" value="1" <?php checked($enabled, true); ?> /> Enable
<?php
}
Step 5: Manage State (Enable/Disable Comments)
Finally, the is_disabled()
method checks whether the user has enabled the “Disable Comments” option in the settings:
private function is_disabled() {
$options = get_option($this->settings_option, ['enabled' => false]);
return isset($options['enabled']) && $options['enabled'];
}
At the very end of your disable-comments-control.php
file, add the following line to instantiate the DisableCommentsControl
class. This ensures all its methods are hooked into WordPress correctly:
new DisableCommentsControl();
Step 6: Activate Your Plugin
- Save all your changes to the
disable-comments-control.php
file. - Go to your WordPress Admin Dashboard.
- Navigate to Plugins > Installed Plugins.
- Locate Disable Comments Control and click Activate.
Step 7: Test Your Plugin
- Go to Settings > Disable Comments and toggle the option to disable comments.
- Check your posts, pages, and custom post types to ensure that comments are hidden and disabled.
- Verify that the Comments menu in the admin area is removed when the option is enabled.
That’s it! You now have a fully functioning WordPress plugin to manage comment settings from the admin panel.
Feel free to customize or expand this plugin based on your needs. Happy coding! 🚀