Remove Comments Section from a Genesis Theme
If you want to remove comments section on a Genesis site it’s unbelievably quick & easy. That’s because Genesis is a WordPress framework and not just a theme – it’s cleverly built to allow you to access pretty much whatever you want using some simple WordPress hooks, filters & actions.
Genesis adds the comment section using an action that inserts the comments template to your page – that means you can simply remove the whole comments section with a single line of code that removes the action.
remove_action( 'genesis_after_post', 'genesis_get_comments_template' )
This removes the comments form – and the whole comments section – from the entire site. It doesn’t simply hide it on the page, it removes it from the markup completely.
Obviously not everyone will want to get rid of the comments box site-wide but there are lots of use cases where doing it conditionally is extremely useful. You could remove the comments section from a single post – or a whole category of posts – if that’s what you wanted.
See the end of the post for some examples of using conditional statements to remove the comment form on certain pages or under conditions.
You can paste that code directly into your functions.php
file and it will remove the comments template but it’s much better practice to wrap it in a function.
Telling WP to execute the function is easy, you just add it as an action and tell WP what point you want to hook it in.
Note that the comments section in Genesis gets added on the genesis_after_post
hook so you need to hook in before then to remove it. Here I’m using the wp_enqueue_scripts
hook but you could use any other hook that executes before the comments section gets added.
function pw_remove_genesis_comments() {
remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}
add_action( 'wp_enqueue_scripts', 'pw_remove_genesis_comments' );
The add_action()
function in the code could also be placed before our custom function – and a lot of people do it that way – but I prefer to write the function first and then add it as an action. Makes more sense to me that way but it does work either way.
Here’s a rundown of what that code does.
- The first line defines the start of the function and gives it the name
pw_remove_genesis_comments
. Everything contained within the curly brackets is what runs when the function is called. - The next line says that we want WordPress to remove an existing action which runs on the
genesis_after_post hook
. It then says the action we want to remove –genesis_get_comments_template
. - The third line – containing just ‘}’ – simply closes the function.
- The final line tells WordPress that we want to add an action and execute it at the point where
wp_enqueue_scripts
runs. The final parameter of that line tells it that the function we are using as the action – it’s thepw_remove_genesis_comments
function we just created.
Conditionally Removing Comments Box from a Genesis Theme
So removing comments from every single page on your site might not be useful to many people. However conditionally removing it will be useful for a lot of you. The WordPress Core offers a collection of conditional tags that make doing this easy.
All that has to be done is to tell WordPress that you only want it to run the code that removes the comments template when a certain condition is met. Those conditions can be almost anything you could think of.
Removing Comments Section from a Single Page
The condition you need to get rid of comments on a single page is easy – it’s just is_single() with the name or ID of the page/post. The following assumes you want to remove them from a post with the ID of ’17’.
function pw_remove_genesis_comments() {
if ( is_single('17') ) {
remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}
}
add_action( 'wp_enqueue_scripts', 'pw_remove_genesis_comments' );
You might not know the page ID so to make this more useful you can also pass it the page slug.
function pw_remove_genesis_comments() {
if ( is_single('about') ) {
remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}
}
add_action( 'wp_enqueue_scripts', 'pw_remove_genesis_comments' );
Or you could use the actual post title if you wanted – which is probably the most user-friendly way of doing it – by adding it to the conditional check.
if ( is_single('About This Company') )
Removing Comments Form from Multiple Pages/Posts
You can pass in an array of post IDs or slugs to is_single() like this:
function pw_remove_genesis_comments() {
if ( is_single(array('about', 'contact') ) ) {
remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}
}
add_action( 'wp_enqueue_scripts', 'pw_remove_genesis_comments' );
Remove Comments From a Custom Post Type
If you wanted to get rid of the comment system on a specific custom post type that’s done slightly differently. It’s done by comparing a variable against the get_post_type()
function. Assuming your custom post type is called ‘book’ you can remove comments from them all using this conditional:
function pw_remove_genesis_comments() {
if ( 'book' == get_post_type() ) {
remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}
}
add_action( 'wp_enqueue_scripts', 'pw_remove_genesis_comments' );
Prevent Comments on posts in a Specific Category
Checking if your on a specific category is just as easy – so you can prevent comments on all posts in a single category using this code.
function pw_remove_genesis_comments() {
if( is_category( '17' ) ){
remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}
}
add_action( 'wp_enqueue_scripts', 'pw_remove_genesis_comments' );
Again this accepts either an ID, slug or the actual category name and can accept an array.
Using the ID: is_category( '17' )
Using the slug: is_category( 'my-category' )
Using the category name: is_category( 'My Category' )
Using an array of category names: is_category( array('My Category', 'Second Category', 'No Comments Category' ) )
Stopping Comments on a Specific Page Template
Many people have custom templates for different page types on their site. If you wanted to drop comment ability on those you could use this code. Substitute ‘template-fullwidth.php’ with whatever file name your template has.
function pw_remove_genesis_comments() {
is_page_template( 'template-fullwidth.php' ) {
remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}
}
add_action( 'wp_enqueue_scripts', 'pw_remove_genesis_comments' );
Help the condition I need isn’t listed here!
Just send me a message through the contact page or leave a comment below telling me what condition you want to remove comments under and I’ll let you know what the conditional tag and syntax you need is.
Shouldn’t is_category( ’17’ ) be if (is_category( ’17’ ))
Oops, yeah it should be, I’ve fixed it in the post now.
Good catch, Tim, thanks for pointing it out đŸ™‚
Thanks. What about comments on media and images? Would this restrict this as well?
We have some spammers that are able to comment on images even though we do not see a comments section when they are displayed.
Thank you
Hey Duayne, this should work on media and images as well. It’s been quite some time since I used this so I’m not 100% certain it’ll work for media as-is – but if it doesn’t feel free to come back and tell me, I’ll do some testing to find the exact piece of code that’ll work for you đŸ™‚
Duayne, those are probably attachment pages for those images. To fix:
Go to Dashboard/Comments and Before you trash the spam comments (for Ugg Boots etc)
Right-click on the actual Page Title in the listings on that Comments section.
Now, open those Titles in a new tab and then click “edit” for each of those wordpress pages.
Scroll to the Bottom and UNCHECK the “trackbacks/pingbacks” box.
Save Page. X that tab as your done with it.
Now, go back to the Comments page in the Dashboard and hit “Trash” for the comment.
Once they are all in the Trash you can either delete the Trash or wait until it auto-deletes.
This may not be your problem, but after turning OFF all comments on a site that had recently been migrated from html over to wordpress where the Comments had once been ON, I discovered spammers finding random Pages and image attachment pages to defile.
Depending on the size of your site, it shouldn’t take too long to either
a) edit all pages and posts systematically from the Dashboard to Uncheck “trackbacks/pingbacks”
b) wait until spammers trickle in to find the image attachments and edit them via Dashboard/Comments as mentioned above
I had to do both but once you go thru a), b) gets less and less until the site is permanently clean.
Good Luck!
Hey Paul,
I think your right that he means post attachment pages. I see on my sites those pages get hit often by spammers. More often by spammers than real users I’d say :p
Great advice to find the pages turn off trackbacks đŸ™‚ I’m sure some people will find it useful to know a way to stop spammers hitting the same page over and over again.
Some sites could probably turn them off sitewide for all attachment pages because I don’t know many sites that have open comments on those pages anyway. In a lot of situations those pages aren’t actually needed – (I don’t make use of them on this site at all) so I have them redirected. In the Yoast SEO plugin you can turn on a setting to redirect those attachment pages directly back to the parent post. The setting is on the Permalinks page.
Good call on the Yoast settings, William. FIt’s under Permalinks as “Redirect attachment URL’s to parent post URL”.(for folks looking for this setting)
And thank you for the Remove Genesis comments completely code. After clearing both wp cache and browser cache I am still seeing this Comments Feed” href=mysite in my page+view+source.
Should I still be seeing that. I’ve added this to the bottom of my functions.php child theme.
remove_action( ‘genesis_after_post’, ‘genesis_get_comments_template’ );
(i added the ; at the end of the code as otherwise it broke the site)
Hello
Thanks to add the new html5 hook in your good post. `genesis_after_post` becomes `genesis_after_entry`
Thanks for pointing that out, Grégoire. Will come in handy since most new genesis themes are html5 ready.
Awesome. Thanks !
I was just getting a lot of blank pages as I was trying your code. If you can adjust the various codes to include html5 that would help a lot….
I have a custom post type movie. A single-movie.php to which I added:
//* Remove comments box
remove_action( ‘genesis_after_entry’, ‘genesis_get_comments_template’ )
But it is not working.
I then tried the following code which I added to my functions file:
function pw_remove_genesis_comments() {
if ( ‘movie’ == get_post_type() ) {
remove_action( ‘genesis_after_entry’, ‘genesis_get_comments_template’ );
}
}
add_action( ‘wp_enqueue_scripts’, ‘pw_remove_genesis_comments’ );
That worked. Awesome! I am looking forward the update of the article.
Have a great day!
Apologies for the blank pages, I can see a missing ‘;’ in the article that would probably be the cause. As for using this for HTML5: someone commented the other day with the solution you need. Just change ‘genesis_after_post’ to ‘genesis_after_entry’.
Note: I haven’t tested that but it should work. If you have any more problems fell free to give me a shout and I’ll do my best to help đŸ™‚
Hi,
I am working with password protected pages for members of a photography club.
I have hooked some custom widgets in the functions.php
And I have a template with custom fields.
My goal is to see these widgets and custom field AFTER I fill in the password.
I use the Genesis framework.
With hook should I use?