0

I am always getting TRUE for current_user_can('edit_comment'); on two different installations of WordPress (4.2.2 and 4.2.4) no matter what role the user is. Even when the user is logged out.

I tested this on a clean WP install and got the same result.

Why would this be?

1
  • I tested it on a third WP install, same thing. This makes no sense to me. All these WP instabilities are getting old and makes me wish I was working under a different platform.
    – Caio Mar
    Aug 18, 2015 at 14:31

2 Answers 2

1

Notice how it is singular? Comment, not comments?

This capability is not designed to check if user can edit any arbitrary comment out there. It can only check if user can edit one specific comment and correct way to call it is current_user_can( 'edit_comment', $comment_id ).

Unfortunately second argument is missing from current_user_can() function signature and it is extremely unobvious that some capabilities must be checked in this fashion.

2
  • Ok. That makes sense, thanks for clearing this up. I will give it a try. Do you know if edit_comment is a core capability? This is unclear to me; I don't think it is. I read some where that WP checks edit_posts capability for comments but I am not sure if they really know what they are saying.
    – Caio Mar
    Aug 19, 2015 at 0:38
  • I don't think there is more generic capability for this. You might have better luck asking new question with details on what you are trying to achieve and troubles you encountered.
    – Rarst
    Aug 19, 2015 at 9:15
0

I think, to debug this scenario, you should first try following to get all the capabilities for the current user and see whether edit_comment actually exists in it or not -

$user_id = get_current_user_id();
$userdata = get_userdata( $user_id );

$allcaps = $userdata->allcaps;

$output = print_r( $allcaps, true );   

echo '<pre>' . $output . '</pre>';

Also, check if moderate_comments suits your requirement instead.

1
  • I checked $capabilities array under the user object and edit_comment is not set. @Rarst pointed out that edit_comment must be checked along with a comment id for this function to work, I will give it a shot soon and return with an update. Thanks for your suggestion!
    – Caio Mar
    Aug 19, 2015 at 0:43

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.