0

I'm trying to do some capability checks on comment authors using the user_can() function, but for some reason it isn't working at all.

I have a custom capability setup with the Role Manager plugin called "read_citizen".

My check looks like this:

if(user_can($commentAuthor->user_id, 'read_citizen') {
   //do stuff here
}

I have confirmed that the $commentAuthor object is set correctly.

Ideas?

12
  • 1
    "Not working" is not saying much. What does not work? What did you expect? What did actually happen? Is there an error in the error log?
    – hakre
    Commented Jul 12, 2011 at 15:03
  • It is supposed to return TRUE if the user has the capability. In this case, nothing is returned.
    – Pippin
    Commented Jul 12, 2011 at 15:12
  • 1
    What if you use a non-custom cap? Does the conditional return true? Also: by "nothing is returned", do you mean that the conditional returns null, or that it returns false? Commented Jul 12, 2011 at 15:36
  • 1
    try using current_user_can('read_citizen') instead
    – Bainternet
    Commented Jul 12, 2011 at 15:42
  • In PHP the value nothing does not exist. What does the function exactly return? FALSE or NULL or even something else?
    – hakre
    Commented Jul 12, 2011 at 16:36

1 Answer 1

2

Ok, found the answer. It was just an oversight on my part.

I was using the wrong variable name for the USER ID. In the $wpdb comment object, the user ID is stored as user_id, but in the USER object, the ID is stored just as ID.

So, by using if ( user_can($commentAuthor->ID, 'read_citizen') ), it all works.

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.