Many of spammers target to media attachments on your WordPress website for comments. You can disable comments on your media attachments by using WordPress plugin or adding custom PHP code in your theme file. Below is the two methods described to disable comments on media attachments.
Advertisement
Method 1 – Using Plugin
Disable Comments WordPress plugin provides and option to disable comments globally on your WordPress website. This plugin allows you to disable all type of comments like post comments, pages comments, and media comments.
Method 2 – Using PHP Code
If you don’t want to use any plugin, You can disable media comments by adding custom code in your WordPress theme. Edit functions.php in your WordPress theme file and append following code. This will disable comments on your media posts.
1 2 3 4 5 6 7 8 | function add_filter_media_comment ( $open, $post_id ) { $post = get_post( $post_id ); if( $post->post_type == 'attachment' ) { return false; } return $open; } add_filter( 'comments_open', 'add_filter_media_comment', 10 , 2 ); |
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.