Advent Calendar- How to disable comments for WordPress pages in any theme

Usually you don’t need comments on pages. Unfortunately, WordPress doesn’t offer a separate option to leave comments on posts on and turn them off for pages. If your theme calls comments_template(); in its page.php and you don’t want to break automatic updates, you cannot just remove the function call, because it will be overwritten with the next update.

No problem. There’s an hook for that. It’s a filter called … wait for it … comments_template. We can change the path to the comments template here – if haven’t figured that out already. Let’s build a small plugin.

So, what do we do? We hook into comments_template(); and change the path. Our new path should point to an existing file without any HTML output. In this case, we use just our plugin file, because we know it exists and we control the output.

As you may have noticed our plugin file is included two times: First as a plugin, later as a replacement for comments.php. The function_exists() check prevents any You cannot redeclare … errors.

Putting this all together …

<?php # -*- coding: utf-8 -*-
/**
Plugin Name: Disable Comments On Pages
Version:     1.0
Author:      Thomas Scholz
Author URI:  http://toscho.de
License:     GPL
*/
// This file is not called from WordPress. We don't like that.
! defined( 'ABSPATH' ) and exit;

// If the function exists this file is called as comments template.
// We don't do anything then.
if ( ! function_exists( 't5_disable_comments_on_pages' ) ) {
	/**
	 * Replaces the path of the original comments template with this
	 * file's path on pages.
	 *
	 * @param  string $file Original comments template file path.
	 * @return string
	 */
	function t5_disable_comments_on_pages( $file ) {
		return is_page() ? __FILE__ : $file;
	}

	add_filter( 'comments_template', 't5_disable_comments_on_pages', 11 );
}

Posted

in

by

Comments

9 responses to “Advent Calendar- How to disable comments for WordPress pages in any theme”

  1. Michael Oeser Avatar

    Nice but I get exactly this error:

    Warning: Cannot modify header information – headers already sent by…

  2. toscho Avatar

    Michael, the critical information is the line number. 🙂

    Could it be an UTF-8 BOM?

  3. Neolot Avatar

    Very nice solution, thanks!

  4. Michael Oeser Avatar

    My bad. Copy&paste without thinking. Removed the space in front of the lines and now it works like a charm.

    Danke!

  5. Rob Avatar

    Nice one.

    To make it work in you functions.php you could use this:

    function disable_comments_on_pages( $file ) {
    return is_page() ? WP_PLUGIN_DIR . “/index.php” : $file;
    }
    add_filter( ‘comments_template’, ‘disable_comments_on_pages’, 11 );

  6. toscho Avatar

    @Rob The index.php in the plugin directory may not exist. In my installations I move this directory to another place usually. And I don’t copy the index.php.
    If you have access to your theme just change the page.php.

  7. […] Advent Calendar- How to disable comments for WordPress pages in any theme December 5, 2011 | Author staff Advent Calendar- How to disable comments for WordPress pages in any theme Unfortunately, WordPress doesn't offer a separate option to leave comments on posts on and turn them off for pages. If your theme calls and you don't want to break automatic updates, you cannot just remove the function call, because it will be … Read more on WPengineer.com […]

  8. […] Die WordPress-Seite wpengineer.com veröffentlicht auch wieder einen Adventskalender. Gestern schrieb Frank Bültge über das einfache Update des WordPress-Core“. Heute beschreibt Thomas Scholz, wie man global für alle Themes Kommentare auf WordPress-Seiten (Pages) deaktiviert. […]

  9. Vic Dinovici Avatar

    hehe, this is an useful snippet. Always wondering why WordPress is making possible commenting on pages, kind of dumb.

    Thanks for sharing!