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…

2 Answers
2

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' );

Leave a Reply

Your email address will not be published. Required fields are marked *