WordPress just lets you enable or disable comments in general or on a per post basis. A common task for a web developer like me is to disable comments and the comment form on pages, attachments or newly created custom post types. In this post I am going to show you how you can do this with a few lines of code.

Level
Theme-Developing
File
functions.php of your Theme
Skills
copy & paste
Plugin
Advanced Comments

Comments and Themes

The following methods work best, when your themes are following WordPress standards implemented starting with WordPress 3.0. But what are those standards?

This is how the current standard theme TwentyFourteen implements comments. You can find the following lines in single.php or page.php.

comments_template()  loads the comments.php file of your theme. There are two things you can do here to disable comments. Removing the comment code from your template files and disabling them by post type in your theme’s functions.php.

Remove comments from template files

The first method to remove comments is to simple remove the lines from above from your template file. Delete them in page.php to not have comments on pages, in single.php to not display them on posts. Have a look at the template hierarchy to find the files from where to remove the code.

If you don’t have files for each post type you can also add a check to the above code in your template file, like this one.

In this example comments for pages are disabled.

I did change the calls to comments in themes on a lot of projects. Sometimes it is better to not tamper with existing template files or create new ones. The next method helps here.

How to disable comments for pages and other post types

In the example above comments_open()  is just a check whether comments are open or not. So to remove comments you might want to return  false  here. Lucky for us, there is a filter for that.

This is the basic code to remove comments from pages and any other post types that you can copy and paste into the functions.php of your theme.

This code allows you to disable comments for any post type and themes where comments_open() is used as a check before displaying comments and the comment form. In my example I removed comments for pages and attachments (line 4). You can add custom post types or even normal posts here as well.

The next block (line 7–8) simply checks if the current post type is among the ones with comments disabled. If so, it returns false.

The last line returns the default value.

Keep in mind that when you allowed comments on a specific post of the post type, they are still disabled by this function when the post type is included here.

If there are already comments made?

Just a word about get_comments_number() from the template code above. This function returns the number of comments. So even if comments_open() returns false, comments will be displayed as long as there are some. I personally prefer to delete comments I don’t want to display anyway, so I am not going to manipulate this test.

Coding insights

The  comments_open  filter used in the code example is called in function  comments_open()  in the file  /wp-includes/comment-template.php. It was first introduced in WordPress 2.5.0.

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *