How to Disable Image Comments in WordPress

How to disable image comments in WordPress

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

Believe it or not, spammers can even comment on your images and media attachments. In this tutorial, you will learn how to disable image comments on your WordPress website.

How is This Possible?

First, as a demonstration, this is an example of how a bot would make a comment on on of your images.

curl --data "author=Tony&email=tony@tonyteaches.tech&url=https://tonyflorida.com/wp-content/uploads/2018/01/mpeg-streamclip-split-vhs.png&comment=Gotcha&comment_post_ID=134" https://tonyflorida.com/wp-comments-post.php

Notice how the spammer passes the required comment metadata to the WordPress API via the wp-comments-post.php file. The key here is the fact that the spammer knows the comment_post_ID. Honestly, this could have been a brute force guess-and-check until getting the ID right.

How to Stop It

In order to turn off comments for images and all media attachments in WordPress, add the following PHP code snippet to your functions.php file. Ideally, you will do this in a child theme.

// Turn off comments for media attachments
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 );

This code disables comments in WordPress on all media attachments.

For a complete tutorial on this topic, check out the following video.

YouTube video

Facebook
Twitter
Pinterest
LinkedIn
Reddit

Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

One Response

  1. Your information is exactly what I was looking for, so thank you very much for providing it. Would you mind telling me what program you use to create your amazing, fast website? For my business, I also want to create a simple website, but I need help deciding on a name and hosting provider. Asphostportal is reputed to have a stellar reputation. Exist any other options? If so, what would you suggest?

Leave a Reply

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