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:
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:
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.
Removing Comments from Attachment Pages in WordPress
attachment.phpand add it to your theme’s folder. You should be able to just copy over thesingle.phpfile. Just be sure to remove the function that displays the comment form. It should end up looking something like this: 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 yoursingle.phpand look for thefunction. Once you find it, just replace it with this: 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 yourfunctions.phpfile: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.Knuckies Hoagies The Interlock
Popular Posts
Hiring a WordPress Consultant: Your Essential Guide to Finding the Right Expert
7 Reasons For A Small Business Website Redesign
The Top 10 Web Design Trends in 2026 (For Small Business Websites)
10 Small Business Website Checklist Wins
Hiring a WordPress Consultant: Your Essential Guide to Finding the Right Expert
7 Reasons For A Small Business Website Redesign
The Top 10 Web Design Trends in 2026 (For Small Business Websites)
10 Small Business Website Checklist Wins
What Does WordPress Management Include?
How to Choose a WordPress Management Company
Small Biz Website Tips Newsletter
Small Biz Website Tips Newsletter