0

I'm trying to write a function which hides the comment form itself, while leaving comments and comment counts intact.

I'd like the comment form to display only when a certain parameter is in a post URL.

All I can find is code which disables everything comment related. And I came up with a method which would require some css (give form another ID when it needs to be hidden and then display: none;).

Does anyone happen to know of a PHP-only method?

2 Answers 2

1

The comments_open() function is used, both by core and typically themes to determine status for specific post. It has filter of same name for result, which you can use to manipulate it individually for the post.

comment_form() won't actually output form if check is false and fire comment_form_comments_closed action in such case.

Native WP themes, shipped with core, are also good examples of building this check into templates.

4
  • Thank you. But the problem with comments_open() is that, when false, it removes everything comment related (incl. comments + count)... not just the form. Seems to be impossible to hide just the form, using a php-only function.
    – Ralph
    Commented Nov 7, 2014 at 17:59
  • @Ralph it shouldn't do that, unless your theme was coded in such way (arguably incorrectly). It should control submission of new comments, not display of existing ones.
    – Rarst
    Commented Nov 7, 2014 at 18:13
  • Thanks for your explanation! I think you're right on the theme's coding. The theme's author suggests I write a check around comment_form(); in the theme's comments.php file. A bit of a pitty, as this code wouldn't be theme update-proof.
    – Ralph
    Commented Nov 8, 2014 at 8:54
  • Update: The reason that solutions based on comments_open() also hide the comment count in my theme, is because comment count is part of the following check: if (get_comments_number() != "0" || comments_open()) in comments.php. So any check in functions.php which outputs false for comments_open(), in a zero comment situation, hides not just the comment form but also the comment count part.
    – Ralph
    Commented Nov 10, 2014 at 14:24
1

Here are two solutions that can be placed in functions.php. They are not really what I need, but perhaps they are of some use to someone else.

Solution 1

Can be used to close comments for certain posts:

function close_comment_form( $open, $post_id ) {
    $post = get_post( $post_id );
    if( $post->post_type == 'post' ) {
        return false;
    }
    return $open;
}
add_filter( 'comments_open', 'close_comment_form', 10 , 2 );

Solution 2

Can be used to hide the comment form via display: none; on CSS selector #HIDEcommentform:

function my_comment_form_defaults($defaults) {
    $defaults['id_form'] = 'HIDEcommentform';
    return $defaults;
}
add_filter('comment_form_defaults', 'my_comment_form_defaults');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.