Minimum Role Not Working
-
Hello
The roles seem to be quite strict.
Right now, a user needs to be the selected role and only that selected role in order to see the menu item.
If I have selected for one role to see this item, but the user belongs to that role among others, then they cannot see the menu item.
Is there a way to work around this?
Thanks
-
WordPress prefers using capabilities for strict checking of who can do what. Roles are unique and by role alone there’s no way to know that one role has the capabilities of another role.
But roles are a lot friendlier for an admin interface. However, sometimes there can be a gap in between how you have your users’ capabilities defined and who can actually see what menu item.
NMR is using
current_user_can( $role )
, but that’s frequently not as clear cut ascurrent_user_can( "manage_options" )
especially as you start getting in to custom roles.The recommendation would be to either add the custom role names as capabilities for other roles. For example, you have defined custom roles “Level 1” and “Level 2”. So you must give Level 2 an explicit capability named the same as the level 1 role. That way if you check
current_user_can( "level-1" )
a Level 2 user would also have access.You could also check the FAQ for info on pseudo roles. If you defined a role or 2 as having the same name as a relevant capability (ex:
edit_posts
, you could probably also achieve a similar thing.This seems like an unnecessary workaround that creates further issues and no point of having differing roles in the first place.
Is there another similar plugin that actually does what is promised?
Could you elaborate on what additional issues are created by either workaround?
here’s an example for adding 2 capabilities as options to the nav menu role UI:
/** * Add custom roles to Nav Menu Roles menu list * @param $roles an array of all available roles, by default is global $wp_roles * @return array */ function kia_new_roles( $roles ){ $roles['edit_posts'] = 'Edit posts'; $roles['manage_options'] = 'Manage options'; return $roles; } add_filter( 'nav_menu_roles', 'kia_new_roles' );
Since they are named after capabilities, this should be all that’s required as
current_user_can( 'edit_posts' )
will happen automatically in the plugin.WordPress itself can’t distinguish between roles… that’s why core uses capabilities. I could write a plugin that shows all caps in the menu admin, but since there are typically a lot more caps than roles (and I don’t see any core functions for grabbing all capabilities either), I decided that’s not a great user experience either.
it’s a tradeoff. this works well for 90% of the people, but obviously that leaves about 10% it won’t be a good fit for.
I don’t know of any plugin that does this with caps only, but I also don’t really need this functionality myself any more so am not actively searching out alternatives.
you are certainly welcome to remix this plugin so that it uses caps… which are admittedly the recommended way to reliably distinguish between users.
I tried adding what you gave me to the site, but it didn’t work. I have a student who has 3 roles. One of the roles is the one a student need to have in order to see the file in their nav menu. But its not working.
What capabilities did you use? What capabilities does your Student role have? I don’t have enough information to write a specific suggestion.
My code snippets was just a broad example.. but I tested
manage_options
which is a cap generally given to admins. As an admin I can see that menu item. as a Customer I cannot. so it appears that concept still works, but you need to be using a cap that is relevant to your setup.You said Role wasn’t good enough since you wanted other roles to see that link too… what is a capability that they share? You can use Member or User Role Editor kinds of plugins to manage caps for each role. Course, if you are using them you can try my other workaround of adding for example “student” to every role that should have access to the “student” menu item.
I’m not that fluent in website stuff, but…
I am using Memberpress Members plugin for creating new roles.
Students have variations of roles based on courses they are taking.
I have giving the role of Group Leader to those students who are able to teach.
The menu item needs to be only visible to group leaders.
However, as the students have other course based roles, they still cant see the “Group Leader” only menu item due to being in other roles.
What is the workaround for this?
As for capabilities, I’m not sure how detailed to get here. Student/Course roles only have forum and course access capabilities.
Group Leaders would have same capabilities but have access to another page which is what I want to show up in the nav menu.
Is there no way for me to fix this?
So it’s new to me that Members allows for a user to have multiple roles. WordPress core only allows one, which is what my plugin is based around.
My plugin is not currently set up to handle that explicitly, though I’m running
current_user_can( $role )
and if$role
is one of the roles they have I would expect the condition to still return true. I haven’t looked at Members since it came under new ownership, so would need to know how they do that.You’re saying a user with both a student role and a group leader role cannot see the menu item, correct? so that might need reviewing at some point, but I couldn’t say when as I have a lot on my plate at the moment and this plugin is largely in maintenance mode.
In the meantime, I would still recommend the “pseudo” role method I suggested earlier.
Let’s say your roles look like this:
Role: Student
Capability: view_the_thing, view_another_thing, etcRole: Group Leader
Capability: do_group_leader_stuff, edit_the_thing, view_something_else, etcTake this snippet and replace
do_group_leader_stuff
with the custom capability that gives a Group Leader access to the special page you mention here:Group Leaders would have same capabilities but have access to another page which is what I want to show up in the nav menu.
/** * Add custom roles to Nav Menu Roles menu list * @param $roles an array of all available roles, by default is global $wp_roles * @return array */ function kia_new_roles( $roles ){ $roles['do_group_leader_stuff'] = 'View Group Leader content'; return $roles; } add_filter( 'nav_menu_roles', 'kia_new_roles' );
Then for the menu item select this new pseudo “role” (pseudo because it’s really a capability). That menu link should now only be visible to the users with whatever capability is unique to group leaders.
I think that would do the trick, but I’m also still not sure how Members is handling the multiple roles per user. So fingers crossed.
-
This reply was modified 3 years, 10 months ago by
HelgaTheViking.
-
This reply was modified 3 years, 10 months ago by
HelgaTheViking.
-
This reply was modified 3 years, 10 months ago by
- The topic ‘Minimum Role Not Working’ is closed to new replies.