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

  • 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 Nov 29 '17 at 17:10
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>';}
  • 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 Nov 29 '17 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. – Daniel Fonda Nov 29 '17 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 Nov 29 '17 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, privacy policy and cookie policy

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