How to Disable Comments on WordPress Using a Custom Function

WordPress is a powerful and flexible content management system that provides users with various options to manage their websites. One such feature is the ability to enable or disable comments on your posts or pages. In some cases, you might want to disable comments globally or for specific post types. In this tutorial, we will explain how to disable comments on WordPress using a custom function. This function will utilize the following code snippet:

 

function filter_media_comment_status( $open, $post_id ) {
    $post = get_post( $post_id );
    if( $post->post_type == 'attachment' ) {
        return false;
    }
    return $open;
}
add_filter( 'comments_open', 'filter_media_comment_status', 10 , 2 );

Step 1: Access Your WordPress Theme’s Functions File

First, you need to access your WordPress theme’s functions.php file. This is where you will add the custom function. To access the file, follow these steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor.
  3. Locate the functions.php file in the right-hand pane, and click on it to open it.

Step 2: Add the Custom Function to Your Functions.php File

Copy the provided code snippet and paste it at the end of your functions.php file. This custom function will filter the comments_open status based on the post type. If the post type is ‘attachment’, comments will be disabled; otherwise, the original comment status will be preserved.

The code breakdown is as follows:

  1. The function filter_media_comment_status() takes two parameters: $open (the current comment status) and $post_id (the ID of the post being checked).
  2. The get_post() function retrieves the post object based on the provided post ID.
  3. The if statement checks if the post type is ‘attachment’. If it is, the function returns false, disabling comments for that post.
  4. If the post type is not ‘attachment’, the function returns the original comment status.
  5. The add_filter() function hooks the custom function to the ‘comments_open’ filter with a priority of 10 and two expected arguments.

Step 3: Save Your Changes

After adding the custom function to your functions.php file, click the “Update File” button at the bottom of the page to save your changes. This will apply the function to your WordPress site.

Step 4: Test the Functionality

To test if the custom function is working correctly, follow these steps:

  1. Navigate to your WordPress admin dashboard.
  2. Create a new post or page or open an existing one.
  3. Attach a media file, such as an image or a PDF, to the post or page.
  4. Publish or update the post or page.
  5. View the published post or page on the front end of your website.

You should notice that the comments section is no longer visible for the attachment post type. However, the comments section will still appear for other post types unless they are explicitly disabled.

Conclusion:

By adding a custom function to your WordPress theme’s functions.php file, you can easily disable comments for specific post types, such as attachments. This tutorial has provided step-by-step instructions on how to implement this function using a simple code snippet. Now you have greater control over the comments section on your WordPress site, enabling you to customize your site’s user experience to better suit your needs.

Don’t Stop Here

More To Explore

Do I Need Web Hosting?

If you’re thinking about starting a website, then you may have come across the term “web hosting” without entirely understanding what that means. When you