0

Is there a way to make it so Everyone can see comments, But only the post author or those belonging to the Premium role can post a comment?

Could I do something like

        global $current_user;
        get_currentuserinfo();

        if (is_user_logged_in() && $current_user->ID == $post->post_author)  {  comment_form();  
    } else {
     if(  $current_user->roles[0] == 'admin' || $current_user->roles[0] == 'premium' )
{ comment_form(); } endif;

     else {
        echo '<h4>You are not allowed to post comments.</h4>';
    }

Sorry I should have said, there are two roles, basic and premium. Both can make posts. But Only premium can comment on anyones posts, and Basic can only comment on their own posts

1
  • So could I do a check on current user is author - Display comments form, else check if current user is premium role - display comments form else display error message?
    – Chazlie
    Commented Nov 29, 2017 at 17:10

3 Answers 3

1

Good news, bad news..

Good news The logic behind it is really simple. You basically wrap the form inside of a simple if statement.

    if(condition){ 
      /*Your form here*/
    } 
    else {echo '<h4>You need to be a premium account to post comments</h4>';}

Bad News You'll need to code in another user role. This can usually be done via a plugin. Most plugins offer documentation on how to check for a user's user-role.

So if the plugin has a function like

get_user_role();

You'd simply place that as your condition. That would look something like this:

  if(get_user_role() == 'premium'){ 
      /*Your form here*/
    } 
  else {echo '<h4>You need to be a premium account to post comments</h4>';}
3
  • Makes sense, Thanks! So what about Post author bit? So If Im the author and you're the premium role, we can both comment on my post. But Bob is a Subscriber so he cant.
    – Chazlie
    Commented Nov 29, 2017 at 15:26
  • Well the simplest implementation would be to just check via if statements, what the role is, who the author of the current post object is, etc. There's a million ways of implementing and developing this I'm afraid. I'd recommend getting a freelancer to land a hand if the budget permits. Commented Nov 29, 2017 at 15:30
  • 1
    To expand on @DanielFonda's comment -- you can get the author's ID from the current $post object; it'll be $post->post_author.
    – Pat J
    Commented Nov 29, 2017 at 15:31
0

In your comments.php template replace comment_form() with this:

if (current_user_can('manage_options') and current_user_can('Premium') {
   comment_form();   
}
0

Replace the comment_form() in comments.php file with this code:

$user = wp_get_current_user();
if(  $user->roles[0] == 'admin' || $user->roles[0] == 'premium' ){
    // show comment form
    comment_form();  
} else {
    echo '<h4>You are not allowed to post comments.</h4>';
}

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.