Disable comments for images / media on WordPress.

Marc Wag­ner

Sep­tem­ber 25, 2020

2 min read|

Word­Press auto­ma­ti­cal­ly crea­tes pages for images in your media libra­ry. On the­se pages, visi­tors can view and com­ment on the image. Not always this is desi­red, and often this is of cour­se exploi­ted by unwan­ted bots. In this tuto­ri­al, I show you how to disable the comm­ents.

Disable WordPress comments with PHP. #

You should not always rely on a plug­in, espe­ci­al­ly for minor pro­blems. Plug­ins often open secu­ri­ty gaps and have to be updated regu­lar­ly, so they pri­ma­ri­ly cost resour­ces and also affect the loa­ding times of the web­site.

Using PHP, you can disable the com­ment func­tion for any Word­Press post type with just a few lines.

function disableCommentsForPostTypes( $open, $post_id ) {
	$listOfDisabledPostTypes = array(
		'attachment',
		//'post',
		//'page'
	);

	$post = get_post( $post_id );

	if(in_array($post->post_type, $listOfDisabledPostTypes)){
		return false;
	}
	return $open;
}
add_filter( 'comments_open', 'disableCommentsForPostTypes', 10 , 2 );

Note: This assu­mes that your the­me uses the comments_open() func­tion to check if comm­ents are allo­wed.

The list of Post Types can be exten­ded as you like. You can quick­ly and easi­ly add more ele­ments after “attach­ment” or remo­ve the slas­hes “//” to include the posts and pages. The slas­hes ser­ve as a one-line com­ment in PHP and are igno­red when the page is loa­ded.

You just have to copy the abo­ve code into the “functions.php” of your child the­me and save it. Now you are done, and unwan­ted comm­ents are dis­ab­led.

Disable WordPress comments with the help of a plugin. #

If you want to use a plug­in despi­te all the war­nings, :) we recom­mend Disable Comm­ents. You can install and acti­va­te the plug­in as usu­al via “Plug­ins > Install” in your Word­Press admin inter­face. The admin page of Disable Comm­ents allows you to sel­ect the post types on which the com­ment func­tion should be dis­ab­led.

image 2
Disable Word­Press comm­ents using Disable Comm­ents plug­in.

Cli­cking “Dele­te Comm­ents” will addi­tio­nal­ly dele­te exis­ting comm­ents and disable com­men­ting.

Note: Dou­ble is not bet­ter in this case. You should not use both vari­ants. Disable Comm­ents uses the same hooks and the­r­e­fo­re the code would just be exe­cu­ted twice.

Did this litt­le gui­de help you? Then lea­ve us a short com­ment. We are hap­py about any feed­back.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar