21

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.

3
  • 1
    Just a friendly reminder i've added an answer to this question that i believe has the desired effect..
    – t31os
    Dec 9, 2010 at 23:54
  • 1
    I'm sure your function works well, but I had already selected an answer that works for my purpose.
    – Mild Fuzz
    Dec 11, 2010 at 18:38
  • Note: Related Trac Ticket #12991 Nov 3, 2011 at 14:24

7 Answers 7

9

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' );
9
  • 1
    this fails when you wish to turn the comments back on. It needs to be edited to that it will run only once
    – Mild Fuzz
    Sep 22, 2010 at 10:12
  • 1
    not at a machine right now, but I guess you can use the metadata mechanic to check for meta, if there not run the function, else add the meta and run the function.
    – Mild Fuzz
    Sep 22, 2010 at 10:23
  • 1
    tested, this isn't going to work as simply as that because the post ID is not contained in the $data var in your function.
    – Mild Fuzz
    Sep 22, 2010 at 10:27
  • 1
    I tested it (with WP 3.0) before submitting my answer and it worked. What issue are you encountering? The post ID is not in $data because the post has not been created yet before the function is run (look at the filter in function wp_insert_post). The function will run only when you visit wp-admin/post-new.php?post-type=xxx, before you enter any content in the fields.
    – sorich87
    Sep 22, 2010 at 10:40
  • 1
    on my test server, I can not turn comments back on, they are perpetually set to off.
    – Mild Fuzz
    Sep 22, 2010 at 13:39
12

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.... :)

2

I've written a plugin to disable comments on pages by default. http://wordpress.org/extend/plugins/page-comments-off-please/

Perhaps that will help?

2
  • 1
    seems excessive given the 5 line solution in sorich87's answer! Cheers though.
    – Mild Fuzz
    Sep 25, 2010 at 20:25
  • 1
    Not really, there's many approaches to tackling code requirements, it's just a different method.
    – t31os
    Nov 17, 2010 at 14:42
1

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.

3
  • 1
    There is already an option there, though. I simply want it off by default.
    – Mild Fuzz
    Sep 21, 2010 at 15:54
  • 1
    There is already an option where? Are you referring to the "Discussion" box at the bottom of the post/page edit screen?
    – EAMann
    Sep 21, 2010 at 16:01
  • 1
    Yes, I just want that to be unchecked. Sorich87 solution almost works. Not at a machine to try anything out yet.
    – Mild Fuzz
    Sep 22, 2010 at 10:16
0

According to this thread, what you're requesting isn't possible without the use of a custom plugin or function to accomplish it.

2
  • 1
    I have no problem writing one, just need to be pointed in the right direction. Hooks & filters etc that may help.
    – Mild Fuzz
    Sep 21, 2010 at 15:53
  • 1
    If you do end up writing one, please share what you did here! Seems like a plugin that can do that would get a lot of use. Sep 21, 2010 at 19:51
0

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.

-2

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.

1
  • 2
    doesn't really answer the (albeit already answered) question
    – Mild Fuzz
    Nov 7, 2011 at 13:05

Your Answer

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

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