I'm trying to determine if a piece of content can be edited by a user. I would like all roles contributor and above to be authorized by a single check. Here's my code:

    if( empty( $post_id ) || !current_user_can('edit_post', $post_id) ) { return; }

Unfortunately, the only users that don't get the return are super admins. Any idea why?

  • How are you getting $post_id? Jun 16 '11 at 1:22
  • Re my comment @HameedullahKhan, I',m afraid I'm not sure why this isn't working. For the benefit of people experiencing a similar problem - you should check that the relevant users have the edit_others_posts capability. Dec 4 '11 at 14:59

And the correct capability name is edit_posts. So the correct way of using current_user_can will be like following:

if( empty( $post_id ) || !current_user_can( 'edit_posts' ) ) { return; }

UPDATE: I have removed the wrong statement, but as the asker mentioned he would like to allow all roles of contributors and above to be authorized for that particular check, I think just checking for edit_posts caps is enough. Sorry for the wrong statement.

  • 2
    This is not entirely correct. Looking at the documentation, current_user_can slices the arguments it's given. It sends the inputted capability and other arguments to $current_user->has_cap() which does something similar. This uses map_meta_cap() to check if user can edit_posts and is post author or can edit_other_posts. This is how WordPress deals with these situations. See wp-includes/capabilities.php for the relevant functions. Dec 4 '11 at 14:55
  • Thanks Stephen for pointing out the mistake. I have removed that incorrect statement. Dec 5 '11 at 15:32

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.