0

By default "Discussion: Allow comments." is checked when a new page or post is created. I know you can make "Discussion: Allow comments." unchecked by default on both pages and posts by going to "Discussion Settings" and unchecking "Allow people to post comments on new articles".

I would like the default to be:

  • On posts: Discussion: Allow comments. (checked by default)
  • On pages: Discussion: Allow comments. (unchecked by default)

I do not want to use a plugin and don't want to modify the main WordPress code. I was hoping I could make a function in my functions.php file to do this?

Something like:

function disable_comments_on_pages() {

}
add_filter( '', 'disable_comments_on_pages' );
1
  • 1
    This is something that should be done in a plugin and not in a theme (you will probably want the functionality to remain after theme switch), And unless you have many pages it will probably be faster and easier to turn comments off per page (your question implies they might be on for some pages) then to write code to have a separate default for pages. Dec 29, 2012 at 6:59

1 Answer 1

0

Hook into wp_insert_post, check if it is an auto-draft for a page, and set the comment_status to closed:

add_action( 'wp_insert_post', 't5_disable_default_comments_on_pages', 10, 2 );

function t5_disable_default_comments_on_pages( $post_ID, $post )
{
    remove_filter( current_filter(), __FUNCTION__ );

    if ( 'auto-draft' !== $post->post_status or 'page' !== $post->post_type )
        return;

    $post->comment_status = 'closed';

    wp_update_post( $post );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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