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:
default_comment_status
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.
Leave a Reply
You must be logged in to post a comment.