Site logo

Archived topic

Disable comments for media attachments

3 replies · Started by Matthew on February 9, 2022

Viewing posts 1–4 of 4

I added the below to Snippets plug-in to disable comments for all media attachments on my site.
Without it, I would be required to go to each media item at Discussion > Allow comments and uncheck the checkbox.
But it isn't working.

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

Any pointers?

Hi Matthew,

Can you try replacing you PHP snippet with this one?:

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

The issue may be occurring because your current code is in uppercase. Kindly let us know if this resolves the issue. :)

Hi Fernando,

Great, this does now cause the removal the comments on the media item front webpage.

However the checkbox in the media edit screen i.e. 'Allow comments' remains checked. Shouldn't it be unchecked?

The PHP snippet you added doesn’t alter the checkbox's attributes, and I can’t see a filter to control this.

However, more importantly, the PHP you added effectively overrides the checkbox ‘Allow comments’. This means that checked or unchecked, because of the php snippet you added, the comments for attachment will be closed.

Hope this clarifies your inquiry! Feel free to reach out anytime if any further assistance is needed. :)

This archived topic is closed to new replies.