How do I turn comments off for pages, but not posts?

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.

Leave a Comment