A Step-by-Step Guide to Disabling Comments on Your WordPress Site

Comments on your WordPress site using the functions.php file. This can be useful if you want to prevent spam or simply prefer not to have comments on your website. Let’s dive right in!

Here’s the code snippet we’ll be using throughout this tutorial:

// Disable comments site-wide
add_filter('comments_open', 'my_disable_comments_status', 20, 2);
add_filter('pings_open', 'my_disable_comments_status', 20, 2);

function my_disable_comments_status()
{
    return false;
}

// Hide existing comment links in the admin bar
add_action('admin_menu', 'my_disable_comments_admin_menu');

function my_disable_comments_admin_menu()
{
    remove_menu_page('edit-comments.php');
}

// Redirect any user trying to access the comments page
add_action('admin_init', 'my_disable_comments_admin_init');

function my_disable_comments_admin_init()
{
    global $pagenow;

    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url());
        exit;
    }
}

// Remove the comments section from the dashboard
add_action('dashboard_glance_items', 'my_disable_comments_dashboard');

function my_disable_comments_dashboard()
{
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}

// Remove the option to allow comments in the writing settings screen
add_action('admin_init', 'my_disable_comments_settings');

function my_disable_comments_settings()
{
    update_option('default_comment_status', 'closed');
}

// Hide comments icon in the WordPress admin top bar
add_action('admin_bar_menu', 'my_remove_comments_icon_from_admin_bar', 999);

function my_remove_comments_icon_from_admin_bar($wp_admin_bar)
{
    $wp_admin_bar->remove_node('comments');
}

Step 1: Access the functions.php file

First, you’ll need to access your theme’s functions.php file. You can do this through your web hosting control panel, an FTP client, or directly within the WordPress admin dashboard under Appearance > Theme Editor. Make sure to back up the file before making any changes.

Step 2: Add the code snippet

Scroll to the bottom of the functions.php file and paste the provided code snippet. This code performs several actions, such as disabling comments site-wide, hiding existing comment links in the admin bar, redirecting users trying to access the comments page, removing the comments section from the dashboard, and updating the default comment status to “closed.”

Step 3: Save your changes

After pasting the code snippet, click “Update File” or save the changes through your preferred method. This will apply the changes to your WordPress site.

Step 4: Verify the changes

Visit your website and confirm that the comments feature has been disabled. You should no longer see comment forms on your posts or pages, and any existing comments should be hidden. Additionally, the comments-related links and sections should be removed from your WordPress admin dashboard.

Step 5: Hide the comments icon in the WordPress admin top bar

The provided code includes a function that hides the comments icon from the admin top bar. This is achieved by adding a new action, my_remove_comments_icon_from_admin_bar, which removes the ‘comments’ node from the admin bar menu.

That’s it! You’ve successfully disabled comments on your WordPress site using the functions.php file. If you ever want to re-enable comments, simply remove the code snippet from your functions.php file and save the changes. Happy blogging!

Posted by

Pablo Eduardo Ibáñez López

Facebook
Twitter
LinkedIn