Remove Comments Section from a Genesis Theme

written by William Patton on April 1, 2014 in WordPress Custom with 13 comments

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.

  1. 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.
  2. 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.
  3. The third line – containing just ‘}’ – simply closes the function.
  4. 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 the pw_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.


Notice: WP_Query was called with an argument that is deprecated since version 3.1.0! caller_get_posts is deprecated. Use ignore_sticky_posts instead. in /var/www/html/wp-includes/functions.php on line 4045