setting comments off as default for pages and custom post types?

I want to turn the comments off by default with pages and custom post-types

Initially I simply used conditionals in the comments display function to avoid displaying the block on these pages, but I need the user to be able to turn the comments back on as required.

Related posts

Leave a Reply

7 comments

  1. From what I understand, you want to set pages and some custom post types to have commenting ‘off’ by default, while posts will still use the default option (i.e. commenting ‘on’). If this is the case, the following function will do it.

    function default_comments_off( $data ) {
        if( $data['post_type'] == 'page' && $data['post_status'] == 'auto-draft' ) {
            $data['comment_status'] = 0;
        }
    
        return $data;
    }
    add_filter( 'wp_insert_post_data', 'default_comments_off' );
    
  2. When you create a new post(custom type or regular), WordPress calls get_default_post_to_edit which then sets the default values a new post will have.

    This function provides a few filter hooks for changing the default title, content and excerpt, but unfortunately nothing else… however each filter passes along the complete $post object to the callback, essentially this means it’s possible to redefine several properties(or just one if we like)

    This should work for you, simply add the post types the code should apply to as cases inside the switch.. (movie and page are examples)..

    function my_default_content( $post_content, $post ) {
        if( $post->post_type )
        switch( $post->post_type ) {
            case 'page':
            case 'movie':
                $post->comment_status = 'closed';
            break;
        }
        return $post_content;
    }
    add_filter( 'default_content', 'my_default_content', 10, 2 );
    

    Code was tested on WP 3.0.1 and has the desired effect of disabling comments for the post types specifically whilst leaving others to inherit default status from the default_comment_status option.

    Hope that helps…. 🙂

  3. I’d recommend still using your conditionals in the comments display, but use options to store your conditional settings. Then you can add a page to the Settings menu that allows administrators to turn comments on or off as needed.


    Update

    It occurs to me that you want to set the default for commenting to “off” as a global setting rather than setting it to “off” each time you create a page. This is possible.

    Go to Settings >> Discussion in the WordPress admin section. In the first section (“Default Article Settings” you’ll see an option called “Allow people to post comments on new articles” … uncheck this option.

    Now, by default, comments will be turned “off” for all new posts, pages, and custom post types. You can still turn comments back “on” on a case-by-case basis by selecting the appropriate checkbox in the “Discussion” meta box on the individual post/page edit screen.

  4. Slightly more elegant way to do this in 2022:

    function disable_comments_on_attachments_by_default( $status, $post_type ) {
        if ( $post_type === 'attachment' ) {
            $status = 'closed';
        }
    
        return $status;
    }
    
    add_filter( 'get_default_comment_status', 'disable_comments_on_attachments_by_default', 10, 2 );
    

    That disables all comment types (comments and pingbacks/trackbacks) on new media library attachments. You can obviously change the post type to whatever you want, or you can include the third parameter in the get_default_comment_status filter to affect only a specific comment type.

  5. If you simply add this code in functions.php

    <?php
    add_post_type_support( 'your_custom_post_type', array( 'comments' ) );
    ?>
    

    Go to the post, and in the screen options, tick on “Discussion” and “Comments”, you can enable/disable comments for each custom post type.