Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I consistently get spam attempts on various and sundry media attachments on my primary WP blog. By default, media has open comments (for instance, http://literalbarrage.org/blog/archives/2009/03/18/daddywill-date-march-2009/dsc08760/), yet there is no native way to disable comments on media files. (e.g. https://skitch.com/zamoose/rhktp/attachmentedit) enter image description here

So, two questions:

  1. How do I disable all comments for future uploads by default?
  2. How do I retroactively disable comments on all previous uploads?

This will help a ton in reducing my incoming spam...

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

This ought to do it:

function wpse15750_comment_check( $id ){
    if( get_post_type( $id ) == 'attachment' )
        exit;
}

add_action( 'pre_comment_on_post', 'wpse15750_comment_check' );

EDIT

Ignore the above. That will stop new comments, but to do what you want, this is much better:

function wpse15750_comments_closed( $open, $id ){
    if( get_post_type( $id ) == 'attachment' )
        return false;
    return $open;
}

add_action( 'pre_comment_on_post', 'wpse15750_comments_closed', 10, 2 );

That will tell WordPress that attachments always have closed comments, but their database values will still say 'open'. If you want to change that, run the following code:

global $wpdb;
$wpdb->update( $wpdb->posts, array( 'comment_status' => 'closed' ), array( 'post_type' => 'attachments', 'comment_status' => 'open' ) );

To prevent any future attachments from having open comments, use the following filter:

function wpse15750_no_attachment_comments( $data ){
    if( $data['post_type'] == 'attachment' )
        $data['comment_status'] = 'closed';
    return $data;
}

add_filter( 'wp_insert_post_data', 'wpse15750_no_attachment_comments' );
share|improve this answer
    
That will handle blocking comment submissions, but it still leaves commenting open on the attachment itself. I would prefer a way to actually disable comments entirely. –  ZaMoose Apr 27 '11 at 14:39
    
deleted. –  kaiser Apr 28 '11 at 3:03
    
@kaiser Indeed. :) –  John P Bloch Apr 28 '11 at 15:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.