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?
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?
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' ),
) );
}
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' );
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' );
}
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
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.
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.
Commented
Oct 9, 2012 at 18:14
$_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...
Dashboard -> Settings -> Discussion
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.
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.