How To Add Flexibility To WordPress Comments Options

When it comes to managing comments in WordPress, the only option at a site level is a big on/off switch which, of course, only works for new posts and pages.

More likely you want more granular control than that. Perhaps you want to switch off comments for pages or but leave them on for standard posts. Or maybe, you’ve spent a great deal of time and effort seeding your site with content only to discover that you forgot to turn off comments and now you’re faced with going through each page and switching them off manually.

In this week’s Weekend Project, we’ll look at how, with minimal effort, you can take control over comments not just for future content but also for existing content.

Composite image of an on-off switch
The built-in WordPress comment settings are little more than a global on off switch

Before we start, let’s quickly recap on the comment functionality that WordPress provides.

The global on/off switch is located in the Default article settings section at the top of the Discussion Settings page (Settings > Discussion)

Screen grab of the default article settings from the Discussion Settings page
The Default article settings are effectively a global on / off switch and only for new content

Crucially, this will affect any new post type, so pages, posts and any custom post types will default to what you set here. It does not change existing content.

As the Settings page tells us, that global setting can be overridden for individual articles. On an item’s edit page you’ll find the Discussion metabox where you can enable and disable comments and trackbacks for that item. The default value is the global setting from the Discussion Settings page.

Screen grab of the discussion metabox from the post edit screen
The Discussion metabox allows you to override the global settings for an individual item

Incidentally, if you don’t see the Discussion metabox then scroll to the top of the edit screen and click on the Screen Options label. A panel will expand and you should see Discussion listed under Show on screen. The checkbox controls whether the discussion metabox appears on the edit screen, so set it accordingly. If Discussion is not listed then that probably means that you are editing a custom post type that does not support comments.

Okay, so out-of-the-box we can globally switch comments on and off for new content and we can override this setting on each post, page and custom post type (if it supports discussion). That’s pretty limited and potentially requires remembering to override global settings at time of publishing (certainly not one of my strong points) so let’s look at how we can extend this and make comment control far more flexible.

Flexible Comment Control? There’s a Plugin For That!

One of the wonders of the WordPress ecosystem is that if you have a WordPress itch then generally there’ll be a plugin available to help you scratch it. In the case of more flexible comment control, I found and tested several but in the end I chose No Page Comment by Seth Alling.

I like this plugin as it allows settings to be applied separately to pages, posts and any custom post types, allows separate control of comments and trackbacks, allows modification of current content and provides access to its functionality via a simple, easy-to-understand, visually-appealing Settings page.

Download and install the plugin and then access the plugin settings via Settings > No Page Comment.

Screen grab of the No Page Comments settings screen
The No Page Comments plugin allows granular control of each post type as well as modifying existing content

As you can see, the Settings page is split into two sections, one for new content and one for current (existing) content. You’ll also notice that Articles is listed – that’s because I’ve got a plugin installed that created this custom post type.

Set the Global Settings For New Content

By default the plugin disables comments and trackbacks on all post types except for Posts, so if you want to change this and check and uncheck the boxes as appropriate and then click on Update Settings. That’s new content taken care of and remember, these options can still be overridden on an individual item.

If you uninstall the plugin, then the WordPress global setting comes back into play for those items that do not have their own discussion setting.

Dealing With Current Content

Now you need to consider what to do with existing content. Unlike the new content settings, these settings are permanent so be sure about what you want to do.

For each post type you have the option to disable and enable comments and trackbacks. What this is actually doing is going through each page or post and changing the discussion setting on the item which is why it cannot be rolled back.

Let’s say, then, that you want comments on pages: simply click on the Disable All Comments button and the Allow Comments option will be set to off for all pages.

What About Existing Comments?

Disabling all comments will prevent further comments on the post type but doesn’t hide or remove any existing comments. If you want to do this then there’s the manual option of moving all the comments you don’t want to Trash in the admin interface or, if there are a lot comments, you can add this code to your theme’s functions.php file:

{code}

function hide_comments( $comments ) {

global $post;

// not in admin, is a page and comments are not allowed
if ( !is_admin() && $post->post_type == ‘page’ && $post->comment_status == ‘closed’ ) $comments = array();

return $comments;
}

add_filter( ‘comments_array’ , ‘hide_comments’ );

{/code}

This code will empty the comments array if we are not in the admin interface, the post type is a page and the comment status is closed, effectively disabling the display of existing comments. Checking for the comment status means that any item-level overrides are respected.

Change the if statement to whatever suits your situation or simply duplicate it if you want to add more tests.

That’s it for this Weekend Project: ten minutes well-spent in adding much needed flexibility to controlling comments on your WordPress site.

Photo Credit: Robert Kevin Moore