5

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

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' );
3
  • 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, 2011 at 14:39
  • deleted.
    – kaiser
    Apr 28, 2011 at 3:03
  • @kaiser Indeed. :) Apr 28, 2011 at 15:15
0

As to No. 1 - remove_post_type_support( $post_type, 'comments' ); needs to be hooked to init.

Codex: https://codex.wordpress.org/Function_Reference/remove_post_type_support

There seems to be comments_open filter as well:

function filter_media_comment_status( $open, $post_id ) {
    $post = get_post( $post_id );
    if( $post->post_type == 'attachment' ) {
        return false;
    }
    return $open;
}
add_filter( 'comments_open', 'filter_media_comment_status', 10 , 2 );

Source: http://www.wpbeginner.com/wp-tutorials/how-to-disable-comments-on-wordpress-media-attachments/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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