How to Disable Image Comments in WordPress

by

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


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment