[Resolved] Check user role is something or above
This support topic is a follow-up to this Trac ticket. I think this is a more appropriate place to continue the discussion, since now it seems it will be more specific to my needs.
Something in my plugin allows users of a certain role or higher to accomplish certain functionality. The minimum role required is set via a drop-down. Anyone with the cap
manage_optionscan change what role or higher is checked against before allowing users into this functionality.
At first I thought I could just check current_user_can(‘WHATEVER_ROLE_IS_SELECTED_AS_MIN’). However, current_user_can(‘editor’) fails for admins (on all but two production blogs – if you have any idea why it would succeed on some blogs (without role plugins), let me know). So that wouldn’t work. Now I have a few routes I could go:
- I could check against the deprecated user levels (1 for contrib, 8+ for admin, etc.). That would give me exactly the functionality I want. However, then I’m using something deprecated.
- I could alternatively pick a capability that each role has, and if that role is selected as the minimum, check against that capability.
- I could loop through the roles in $wp_roles->get_names and allow all roles up to and including the selected role. However, that’s depending on the ordering of that array, which while it should be fine, probably isn’t dependable.
- Finally, I could just create my own new capability and assign it to the minimum selected. However, that would make it slightly tricky to make sure the caps are all in place when a user changes the selection. Or if I just made my users install a role-management plugin, that would make their lives more difficult.
Is there some ideal way of doing this? If I go with the 2nd solution, which capabilities should I be choosing to check against for each role?
- The topic ‘[Resolved] Check user role is something or above’ is closed to new replies.