0

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

When I create a new page, I want these two checkboxes (from the "Discussion" meta box) to appear unchecked by default.

Can this be done programmatically?

"Discussion" meta box

2
  • I'm assuming you already figured that turning off comments on the setting page won't work for your case? Commented Apr 5, 2012 at 23:25
  • No. They should appear unchecked for the "page" post type only Commented Apr 5, 2012 at 23:26

4 Answers 4

2

You can re-register the "page" post type and leave out the comments capability.

add_action( 'init', 'my_new_page_type' );

function my_new_page_type() {
    register_post_type( 'page', array(
        'labels' => array(
            'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
        ),
        'public' => true,
        'publicly_queryable' => false,
        '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
        'capability_type' => 'page',
        'map_meta_cap' => true,
        'hierarchical' => true,
        'rewrite' => false,
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'revisions' ),
    ) );
}

Actual answer: original source here

function default_comments_off( $data ) {
    if( $data['post_type'] == 'page' && $data['post_status'] == 'auto-draft' ) {
        $data['comment_status'] = 0;
        $data['ping_status'] = 0;
    }

    return $data;
}
add_filter( 'wp_insert_post_data', 'default_comments_off' );
7
  • You'll still probably want to manually deactivate comments for all existing pages first. Commented Apr 5, 2012 at 23:32
  • Didn't try it yet, but won't that completely disable the ability to comment? Commented Apr 5, 2012 at 23:34
  • Yeah, I figured that was the goal, but now I see that you simply just want it to default off with the option to turn them on. Not sure about that one yet. Commented Apr 5, 2012 at 23:35
  • 2
    Here we go: wordpress.stackexchange.com/a/2082/3293 Commented Apr 5, 2012 at 23:36
  • That solution seems to work, thanks! You should update your answer or vote to close this Q as dupe Commented Apr 5, 2012 at 23:44
4

Re-registering the page post type is a bit excessive. remove_post_type_support is a bit easier. Combined with the answer mentioned by @developdaly, and you'll effectively turn comments off.

<?php
add_action( 'init', 'wpse48145_remove_comments', 99 );
/**
 * Removes support for comments from Pages.
 * 
 * @uses remove_post_type_support
 */
function wpse48145_remove_comments()
{
    remove_post_type_support( 'page', 'comments' );
}
3
  • I think this should be the 'correct answer' - a lot neater. Commented Apr 6, 2012 at 8:29
  • @StephenHarris No one read the question! :D (And I even upvoted them!).
    – kaiser
    Commented Oct 9, 2012 at 18:56
  • I fairly certain the question changed since I posted this answer? Commented Oct 9, 2012 at 20:02
3

This has/had annoyed me for a while so I did some digging around:

function wpse48145_unchecked_page_discussion () {
      if( 'page' == $_REQUEST[ 'post_type' ]  ) {
        add_filter( 'pre_option_default_comment_status', 'wpse48145_unchecked_page_discussion_filter' );
        // & if you want...
        add_filter( 'pre_option_default_ping_status', 'wpse48145_unchecked_page_discussion_filter' );
    }
}
add_action( 'load-post-new.php', 'wpse48145_unchecked_page_discussion' );

function wpse48145_unchecked_page_discussion_filter ( $val ) {
    return 'closed';
}

works as of WP 3.4.2

4
  • +1 But please use get_post_type() instead of $_REQUEST['post_type']. Also: You don't have to state the priority and var number on add_filter/*_action calls when it's only one argument.
    – kaiser
    Commented Oct 9, 2012 at 12:24
  • get_post_type() is not available by that time. I just followed the methodology in wp-admin/admin.php where the load-{page} action is triggered.
    – Dankicity
    Commented Oct 9, 2012 at 18:14
  • 1
    $_POST doesn't work there, only $_REQUEST or more appropriately, $_GET. Tried to test get_post_type even with passing globals and no dice.. looks like $_REQUEST/$_GET is the only way...
    – Adam
    Commented Oct 9, 2012 at 18:49
  • I now added the solution to the trac ticket, that @ChipBennet posted.
    – kaiser
    Commented Oct 9, 2012 at 18:56
1
  1. Go to Dashboard -> Settings -> Discussion
  2. Find Default Article settings
  3. Uncheck "Allow link notifications from other blogs (pingbacks and trackbacks)" and "Allow people to post comments on new articles"

These changes will not change settings for existing posts. If you want to change existing posts, do a Bulk Edit from the Dashboard -> Posts screen.

Edit

Sorry, I misread your question initially. WordPress doesn't provide a UI to set default comment status based on post type.

Note: this is a known issue.

2
  • This also affects posts. I only need it for pages Commented Apr 5, 2012 at 23:25
  • Sorry; misread your post. See update, and linked Trac bug report. Commented Apr 5, 2012 at 23:58

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