Site logo

Archived topic

Dissable Comments

14 replies · Started by Eaydman on May 18, 2020

Viewing posts 1–15 of 15

Hi,
I want to hide/delete the comment section from the post type.

How can I do that ?

Thanks David :)

You're welcome

I have a CPT 'wb_event'. I want to disable comments for the single page.

The "Disable Comments" plugin works. Surely, however, there's a way to do this with code.

I've tried:
1. removing "comments" from the "supports" arg, where the CPT is defined
2. Using a function that does a remove_post_type_support('wb_event', 'comments'), with:
add_action('init', {function_name}). I've tried this both in the child theme's functions file and in a file in mu-plugins.

Neither works.

Please advise. Thanks.
Jeff Cohan

Feranando -

I had read that article prior to adding my above comment. It's what steered me to the "remove_post_type_support()" solution — which I've tried but which does not work.

Can you think of any reasons remove_post_type_support() would not work?

Here's my code, currently in a file in mu-plugins/:

add_action( 'init', 'remove_custom_post_type_comments' );
function remove_custom_post_type_comments() {
	remove_post_type_support( 'wb_event', 'comments' );
}

I think the answer to my question is that remove_post_type_support() applies to the edit screen.
Also, the comments feature apparently applies only to diplaying comment count.

So, now I'm trying this:

add_action ('init', 'jdc_remove_comment_template');
function jdc_remove_comment_template() {
	if ( ! is_singular( 'wb_event' ) ) return;
	remove_action( 'generate_after_do_template_part', 'generate_do_comments_template', 15 );
}

Although the if condition isn't working, the remove_action line does work if I comment out the if condition.

Hi there,

what if you try this for your condition

if ( 'wb_event' !== get_post_type() )

David -

I did try that, too.
if ( 'wb_event' != get_post_type() ) return;
Still no love.
The code is now in child theme's functions.php
I'm thinking:

1. I might be using the wrong hook for the remove_action(). Thoughts?

2. I might be using the wrong hook or priority for the add_action()
I've also tried after_theme_setup and wp_loaded. If so, it's not a GeneratePress question...

I think I solved it by changing the hook for the add_action to "get_template_part_content".

I say "think" because I need to investigate and confirm that the comments template hasn't been removed for other post types; but an initial quick inspection indicates success.

Thanks!

See my reply above :) Yours too will work as thats firing real late in the sequence.

Glad to hear its working

David -

I just now saw your 9:22am comment. Thank you.

I do see what you mean (that the "init" is the correct hook beause the comments form IS removed if I disable the post-type condition and use init as the hook for the add_action). But when I do that, the comments form is removed for BLOG POSTS, too (and I'm assuming for other post types).

The only thing that's worked for me so far (i.e., comments form removed ONLY FOR this CPT) is when I used the get_template_part_content as the hook for the add_action.

I doesn't make sense to me. But as you suggested, if my solution isn't broken, I'm not going to spend more time trying to fix it.

Cheers,
Jeff

Glad to hear that is working !

This archived topic is closed to new replies.