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.
Share this:
Email
Facebook
Twitter
Pinterest
Pocket
Premium Themes for WordPress
Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.
Use this WordPress website builder to build powerful websites in no time for your or your clients.
WordPress Hosting
WP Engine – designed from the ground-up to support and enhance the workflow of web designers.
Bluehost – providing quality web hosting solutions since 1996.
Removing Comments from Attachment Pages in WordPress
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 thesingle.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 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 your
functions.php
file: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.
Share this:
Premium Themes for WordPress
Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.
Use this WordPress website builder to build powerful websites in no time for your or your clients.
WordPress Hosting
WP Engine – designed from the ground-up to support and enhance the workflow of web designers.
Bluehost – providing quality web hosting solutions since 1996.
Related
AppSumo
WPMUDEV
MemberPress
About the author
Luke Perrie
Premium WordPress Themes by bavotasan.com
Launchstream
Tienda
Destin