0

I have a need to disable comments for a specific group of users. But I want to replace the entire comment box with a message like "Sorry, you are not allowed to enter a comment".

Other users should still be able to enter a comment in the normal way.

I don't think I found a filter to do that, but I may be looking in the wrong area of comments.php.

Is there a way to replace the entire comment area with a message?

Added

This ability is being added to a plugin, so it needs to be theme-independent - effective on all sites where the plugin is enabled.

1 Answer 1

1

Something like this should be pretty universally functional across themes, as long as they use something like the normal comment form code.

if ( ! current_user_can( $capability_required_for_commenting ) ) { 
    add_filter('comments_open', 'disable_comments', 20, 2);
    add_action('comment_form_comments_closed', 'display_no_comment_permissions_message' );
}

And then somewhere you'll want to define:

function disable_comments() {
    return false;
}

function display_no_comment_permissions_message() { ?>
    <p><?php _e( 'Sorry, but you aren’t able to comment!', 'text-domain' ); ?></p>
<?php }

Probably the place you want to add this is somewhere around the call to comment_form().

4
  • Your solution would be effective, except my needs is more global, and not theme-dependent, as it is a function of a plugin I am writing. I updated the question with that info. Thanks. Sep 29, 2022 at 6:11
  • Ah, ok. You could hide the comment form with CSS (target #respond) and hook your message into the comment_form_before action — that ought to work OK, although maybe there's a better solution than CSS here. Sep 29, 2022 at 15:45
  • Got it — updated the answer. Sep 29, 2022 at 15:54
  • This looks really good - and is simple to implement with an 'if' statement to add the actions. It might even be more simplified with a add_filter('function_name', function code.....) thing. But exactly what I was looking for. Sep 29, 2022 at 20:31

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.