Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
Just a friendly reminder i've added an answer to this question that i believe has the desired effect.. –  t31os Dec 9 '10 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 '10 at 18:38
    
Note: Related Trac Ticket #12991 –  Chip Bennett Nov 3 '11 at 14:24

6 Answers 6

up vote 8 down vote accepted

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' );
share|improve this answer
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 '10 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 '10 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 '10 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 '10 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 '10 at 13:39

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

share|improve this answer

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?

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

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

share|improve this answer
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 '10 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. –  tnorthcutt Sep 21 '10 at 19:51

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.

share|improve this answer
1  
There is already an option there, though. I simply want it off by default. –  Mild Fuzz Sep 21 '10 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 '10 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 '10 at 10:16

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.

share|improve this answer
2  
doesn't really answer the (albeit already answered) question –  Mild Fuzz Nov 7 '11 at 13:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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