Quick Tip: Disable Comments Programmatically

October 30, 2012 — 6 Comments

If you're an experienced WordPress Developer, then skip down to the code.

I’ve been working on small site that’s serving as a digital storefront for a company. The requirements have called for the homepage to feature a variety of featured projects, a news feed, and similar features all of which are obviously custom queries.

There’s a unique feature to this particular project around the newsfeed that’s a bit atypical for other blogs. That is, the project calls for comments and pings to be disabled.

If you find yourself in a similar situation, here’s a quick tip for how you can disable comments programmatically and how you can disable pings programmatically.

WordPress stores settings for comments and pings in two options:

  1. default_comment_status
  2. default_ping_status

To disable these options, all that’s really required is that you set them equal to an empty string:

  • update_option( 'default_comment_status', '' );
  • update_option( 'default_ping_status', '' );

Of course, there’s more to it than that. Assuming that you’re working on a theme for this, you’d want to wire this up to a specific hook and only do so after the theme as been activated.

Disable Comments Programmatically

From a high-level, this is how I organize my function:

  • I want to fire a function after the theme has been setup. The after_setup_theme works for this.
  • I only want to change the option if it has been enabled.
function example_disable_all_comments_and_pings() {

	// Turn off comments
	if( '' != get_option( 'default_ping_status' ) ) {
		update_option( 'default_ping_status', '' );
	} // end if

	// Turn off pings
	if( '' != get_option( 'default_comment_status' ) ) {
		update_option( 'default_comment_status', '' );
	} // end if

} // end example_disable_all_comments_and_pings
add_action( 'after_setup_theme', 'example_disable_all_comments_and_pings' );

Simple enough, isn’t it?

Of course, if you want to disable comments and pings for existing posts, that’s another post.

6 responses to Quick Tip: Disable Comments Programmatically

  1. small hint to a plugin, there can work for this job with all parts for deactivate/hide comments and his sections: https://github.com/bueltge/Remove-Comments-Absolutely

  2. Welcome and thanks for your “Killer” Assessment

  3. Did you get your comments flip flopped? :)

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>