I’m not too sure what’s been happening over the past week but all of a sudden I’ve been getting tons of spam comments from my attachment pages. I didn’t actually know I had attachment pages but I guess WordPress creates them by default within the template hierarchy. Problem is, there’s no way to turn off the comment section. Though I did figure out a few ways to work around that.

If you don’t mind creating a new file, create one named attachment.php and add it to your theme’s folder. You should be able to just copy over the single.php file. Just be sure to remove the function that displays the comment form.

It should end up looking something like this:

<?php get_header(); ?>

	<div id="primary" <?php gridiculous_primary_attr(); ?>>

		<?php
		while ( have_posts() ) : the_post();
			get_template_part( 'content', 'page' );
		endwhile; // end of the loop.
		?>

	</div><!-- #primary.c8 -->

<?php get_footer(); ?>

That’s an example of how the Attachment page template would look in Gridiculous Pro. By creating a new page template, you allow yourself the option to customize it however you like, so that might be the right solution for you.

If you’d rather just turn off the comment section without having to create a new file, there are two approaches.

The easiest would be to just open up your single.php and look for the function. Once you find it, just replace it with this:

<?php
if ( 'attachment' != get_post_type( get_the_ID() ) )
     comments_template( '', true );
?>

Or you could use one of the WordPress action hooks to remove the comment section programmatically. This can be done by adding the following to your functions.php file:

add_action( 'pre_comment_on_post', 'remove_comments_from_attachments', 10, 2 );
/**
 * Function to remove the comment section from all attachment pages
 * 
 * @param  $open
 * @param  $post_id
 */
function remove_comments_from_attachments( $open, $post_id ){
    return ( 'attachment' == get_post_type( $post_id )  ) ? false : $open;
}

Whichever way you choose to remove comments from your attachment pages is up to you, though it should be something you look into if you start to see the number of spam comments coming in increase.