0

Like for example, I have 5 users I want to give permission to comment, but no one else.

But everyone needs to be able to read them. (Publicly viewable)

Like a blog about a show with 5 actors and I only want them to be able to comment on this specific page. This is for a custom page template.

I think I could use this action hook, but I can't find any examples by searching.

768  *
769  * @param int $post_id An optional post ID to check instead of the current post.
770  * @return bool True if the comments are open
771  */
772 function comments_open( $post_id = null ) {
773 
774      $_post = get_post($post_id);
775 
776      $open = ( 'open' == $_post->comment_status );
777      return apply_filters( 'comments_open', $open, $post_id );

Would it be something as simple as this pseudo code?

if my_user_can_post_comments = true {
     add_filter('comments_open', true, $ID);
} else {
     add_filter('comments_open', false, $ID);
}
1
  • The main trouble I was having was with the action hook and actually being able to set it's enabled or disabled status per condition. Thank you for the help! Commented Jun 26, 2013 at 14:43

2 Answers 2

3

You can check for a user role. This should work when you put the code into the template above the call to comments.php:

if ( current_user_can( 'actor' ) ) // or use 'publish_posts'
    add_filter( 'comments_open', '__return_true' );
1
  • add_filter( 'comments_open', '__return_true' ); That worked! Thanks, it was getting it to return true properly that wasn't working for me. I'm slowly getting better with Callbacks. Commented Jun 26, 2013 at 14:37
2

If you know the ID of the users who are allowed to comment I don't see why you can't do something like this.

 $current_user = wp_get_current_user(); //get data of logged in user
 $user_ID = $current_user->ID; //current users id
 $allowed_users = array(3,6,9,10,33); //array of users who are allowed to comment

 if(in_array($user_ID, $allowed_users)) {
       add_filter('comments_open', true, $ID);
 } else {
        add_filter('comments_open', false, $ID);
 }

To be honest, not sure if the filter code itself will work. If you provide more details maybe I can tell you for sure. However, the logic is good and hopefully puts you on the right track. It basically just uses PHP's in_array and check an arrayed of allowed ID's against the logged in users id.

You may also be able to wrap comment_form();

1
  • Yea, the issue isn't checking if the user has permission, it's disabling the comment form. Commented Jun 26, 2013 at 14:23

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.