I want my pages to not have comments enabled by default, only my posts. Is this possible?
-
1How to disable comments for WordPress pages in any theme.– fuxia ♦Aug 26, 2012 at 21:08
-
@toscho An interesting idea - should be an answer instead.– kaiserAug 27, 2012 at 10:46
-
@kaiser I did that. Two times.– fuxia ♦Aug 27, 2012 at 14:42
-
@toscho In this case, just tell me where to move my answers and then close this question.– kaiserAug 27, 2012 at 16:27
-
The other questions are either bad or not a duplicate. We leave it as it is for now.– fuxia ♦Aug 27, 2012 at 16:28
3 Answers
The Front-End Solution:
Trick your templates
You could fake that comments are open for specific post types. The following code works for all templates, that wrap the comment form (or whatever comment related code/UI elements) inside a conditional comments_open()
check.
The little magic
… wrapped up in a quick'n'small plugin.
/**
* Plugin Name: Disable comments for post types
* Author: Kaiser
* Author URl: http://unserkaiser.com
*/
/**
* Set comments open to FALSE for specific post types
*
* @param bool $open
* @param int $post_id
* @return bool $open
*/
function wpse63098_diable_comments( $open, $post_id )
{
if ( ! $open )
return $open;
$curr_post = get_post( $post_id );
if ( in_array(
$curr_post->post_type
,array(
'page'
// Add other (custom) post types here
)
) )
return FALSE;
return $open;
}
add_filter( 'comments_open', 'wpse63098_diable_comments', 20, 2 );
How to use it
Just upload and enable the plugin in the administration. Now every time the template questions for something like…
if ( comments_open() )
{
// show comment form or whatever
}
…the comment_open()
function gets intercepted and spits out a FALSE
for your post type (like for example pages). Then it simply skips whatever is wrapped in there and doesn't show comments.
-
In wordpress 4.6.1 this gives a warning:
Warning: Missing argument 2 for wpse63098_diable_comments()
– UserSep 9, 2016 at 17:49
The Back-End Solution:
Javascript to set the default to unchecked
When you got the "Discussion" Meta Box open, then both checkboxes (Comments & Pingbacks) will be set to the value from…
Admin UI » Settings » Discussion » Discussion Settings » Default article settings » "All people to post comments on new articles"
A plugin to change the defaults
As we can't simply set different values for different post types, we can rely on javascript (which won't work, if JS is turned off - for example in a screen reader).
Now the meta box offers a hook, where we can add this little javascript snippets.
<?php
/**
* Plugin Name: Uncheck comments for post types
* Author: Kaiser
* Author URl: http://unserkaiser.com
*/
/**
* Uncheck ping/comments open status checkboxes for specific post types
*
* @param object $post
* @return void
*/
function wpse63098_disable_comments_cb( $post )
{
if ( ! in_array(
$post->post_type
,array(
'page'
// Add other (custom) post types here
)
))
return;
?>
<script type="text/javascript">
jQuery( document ).ready( function($) )
{
// Comments
if ( $( '#comment_status' ).is( ':checked' ) )
$( '#comment_status' ).attr( 'checked', false );
// Pings
if ( $( '#ping_status' ).is( ':checked' ) )
$( '#ping_status' ).attr( 'checked', false );
}
</script>
<?php
}
add_action( 'post_comment_status_meta_box-options', 'wpse63098_disable_comments_cb' );
You probably just need to show the widget for Discussion.
1 go to Edit a Page and
2 select the Screen Options.
3 Check the Discussions box
4. Close / hide screen options.
5. Scroll down you page and you'll see the new Discussion widget
6. uncheck Allow Comments
[edit ... i was going to show another image here ... but suddenly the upload failed]
-
1This is how to hide show comments in the backend, but it doesn't remove it from the front-end.– gdanielAug 5, 2014 at 19:53