How do I turn comments off for pages, but not posts?

I want my pages to not have comments enabled by default, only my posts. Is this possible?

Related posts

Leave a Reply

3 comments

  1. The Front-End Solution:

    Trick your templates

    You could fake that comments are open for specific post types. The following code works for all templates, that wrap the comment form (or whatever comment related code/UI elements) inside a conditional comments_open() check.

    The little magic

    … wrapped up in a quick’n’small plugin.

    /**
     * Plugin Name: Disable comments for post types
     * Author: Kaiser
     * Author URl: http://unserkaiser.com
     */
    
    /**
     * Set comments open to FALSE for specific post types
     * 
     * @param  bool $open
     * @param  int  $post_id
     * @return bool $open
     */
    function wpse63098_diable_comments( $open, $post_id )
    {
        if ( ! $open )
            return $open;
    
        $curr_post = get_post( $post_id );
        if ( in_array(
             $curr_post->post_type
            ,array(
                'page'
                // Add other (custom) post types here
             )
        ) )
            return FALSE;
    
        return $open;
    }
    add_filter( 'comments_open', 'wpse63098_diable_comments', 20, 2 );
    

    How to use it

    Just upload and enable the plugin in the administration. Now every time the template questions for something like…

    if ( comments_open() )
    {
        // show comment form or whatever
    }
    

    …the comment_open() function gets intercepted and spits out a FALSE for your post type (like for example pages). Then it simply skips whatever is wrapped in there and doesn’t show comments.

  2. The Back-End Solution:


    Javascript to set the default to unchecked

    When you got the “Discussion” Meta Box open, then both checkboxes (Comments & Pingbacks) will be set to the value from…

    Admin UI » Settings » Discussion » Discussion Settings » Default article settings » “All people to post comments on new articles”

    A plugin to change the defaults

    As we can’t simply set different values for different post types, we can rely on javascript (which won’t work, if JS is turned off – for example in a screen reader).

    Now the meta box offers a hook, where we can add this little javascript snippets.

     <?php
     /**
     * Plugin Name: Uncheck comments for post types
     * Author: Kaiser
     * Author URl: http://unserkaiser.com
     */
    
    /**
     * Uncheck ping/comments open status checkboxes for specific post types
     * 
     * @param  object $post
     * @return void
     */
    
    function wpse63098_disable_comments_cb( $post )
    {
        if ( ! in_array( 
             $post->post_type
            ,array(
                'page'
                // Add other (custom) post types here
             )
        ))
            return;
        ?>
        <script type="text/javascript">
        jQuery( document ).ready( function($) )
        {
            // Comments
            if ( $( '#comment_status' ).is( ':checked' ) )
                $( '#comment_status' ).attr( 'checked', false );
    
            // Pings
            if ( $( '#ping_status' ).is( ':checked' ) )
                $( '#ping_status' ).attr( 'checked', false );
        }
        </script>
        <?php
    }
    add_action( 'post_comment_status_meta_box-options', 'wpse63098_disable_comments_cb' );
    
  3. You probably just need to show the widget for Discussion.

    enter image description here

    1 go to Edit a Page and
    2 select the Screen Options.
    3 Check the Discussions box
    4. Close / hide screen options.
    5. Scroll down you page and you’ll see the new Discussion widget
    6. uncheck Allow Comments

    [edit … i was going to show another image here … but suddenly the upload failed]