Skip to content

How to Disable Comments on Attachments in WordPress

How to Disable Comments on Attachments in WordPress. This tutorial will help you to disable comments on media attachments on WordPress site

Category
Web Servers
Covers
WordPress
On this page

Disabling comments on attachments in WordPress can be achieved in two primary ways: using a plugin and by adding custom PHP code to your WordPress site. Both methods have their own advantages and use cases, and I will provide detailed instructions on how to use each method.

Method 1 – Using a Plugin

The Disable Comments WordPress plugin offers a feature to universally disable comments throughout your WordPress website. With this plugin, you have the ability to turn off various types of comments, including those on posts, pages, and media attachments.

Disable Comments on Attachments

Method 2 – Using Custom PHP Code

For those who are comfortable with coding, or do not want to use a plugin, adding custom PHP code is a viable option. This method involves editing your theme’s functions.php file.

  1. Access the Theme’s Functions.php File:
    • Go to ‘Appearance’ > ‘Theme Editor’ in your WordPress dashboard.
    • Select your active theme.
    • Find and click on the functions.php file.
  2. Insert Custom Code: Add the following PHP snippet at the end of the functions.php file:

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

    This code checks if the post type is an attachment and disables comments for it.

  3. Save the Changes: Click on 'Update File' to save your changes.

Things to Consider

  • Backup: Always backup your WordPress site before making changes, especially when editing PHP files.
  • Child Theme: If you're editing functions.php, it's recommended to use a child theme to prevent losing your changes when updating the parent theme.
  • Testing: After making changes, test your website to ensure that everything is working as expected.

Conclusion

Disabling comments on attachments in WordPress can enhance your site's user experience and control unwanted spam. Whether you choose a plugin for simplicity or custom code for more control, both methods are effective. Remember to backup your site before making changes and test thoroughly after implementing the new settings.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

  1. Thanks Rahul,

    Also remember to create and activate a child theme before doing this so any updates to the theme will not wipe out your hard work.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top