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.
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.
- 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.
- 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.
- 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.
1 Comment
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.