You shouldn’t use Roles to handle permissions for your menu page. Instead, use capabilities, and then assign those capabilities to whichever roles should have access.
For example, instead of creating a customrole
role, use a custom capability like manage_custom_plugin
:
add_menu_page(
'Custom-Plugin',
'Custom-Plugin',
'manage_custom_plugin',
'custom-plugin',
'init_custom_menu_page'
);
Now, using the Members plugin you can enter manage_custom_plugin
into the “Custom Capability” box, and add it to whichever roles you need.
In code you would grant this capability to roles using WP_Roles::add_cap()
, like so:
$roles = wp_roles();
$roles->add_cap( 'administrator', 'manage_custom_plugin' );
$roles->add_cap( 'editor', 'manage_custom_plugin' );
Just be aware that these functions write to the database, so should only be run once, on plugin or theme activation.
Related Posts:
- How to change user role setting in members plugin so that user can only edit his own post?
- wp_update_user not updating
- Is WordPress’ is_user_logged_in() secure?
- Temporarily disable user role login and replace with message
- how to add custom user capabilities using add_user_meta or something else?
- Is there a way to set the user Role based on email domain
- Adding multiple user roles dynamically
- pre_get_posts Remove posts based on meta value with ‘post__not_in’
- What is the difference between “create_users” and “add_users” capabilities?
- Access on specific pages in wordpress for a specific user
- How to ‘unpublish’ or ‘hide’ posts when user role changes?
- Hide Specific User Page
- Let new user role to ‘edit_others_posts’ of other user role, not of its own type
- How can I have different groups of editors only allowed to edit certain parent+subpages?
- Capabilites not working [closed]
- How do I remove the Other Roles field (from User Role Editor plugin) in wp-admin/user-new.php
- How to redirect specific post type with user role
- wordpress editor role remove all but ‘menus’ in appearance menu
- Parent User and Child User – relate users
- How to create user specific pages (not user role!)?
- Assigning certain authors to specific editors
- Restrict Access in Admin Panel
- Limit a user to have access to only specified pages?
- Plugin creation – how to add user rights?
- User restricted only show posts assigned to current user
- Restrict custom post content to specific user
- Hide front-end from every logged out user and redirect them to the default login page
- How to restrict an admin page, if the user is not superadmin?
- Need to block user role from accessing bbPress all together
- How to create a front facing user sign up, log in and profile pages like FoodGawker.Com [closed]
- Infinite redirects at front end if logged in user is not an Admin (Toolset Access)
- User role and capablities only for 1 plugin
- Block access to plugins.php for custom role
- Is there a way to allow users with “Subscriber” user role to access media library in backend and frontend
- How to make WP page accessile only to specific user roles
- Public WP website with one area just for members
- How to allow suscriber to access specific pages in dashboard?
- Check what capabilitie(s) an action requires
- Custom User Role: Can Edit Own Page, Cannot Create New
- Custom Admin Menu Order for all User Roles
- Removing Admin Bar Node Based on Role
- Is it possible to restrict a specific user to edit a specific custom post.
- Grant access to admin menu?
- New folder and file permissions are not correct
- How to change a user’s role?
- Allow member to have access to custom post type only. Permission to only edit their own posts
- Make A WordPress Page Accessible To Admins Only, Redirect Other User Roles
- How to allow registered users to change their user role through frontend?
- if role is logged in then do something
- how to change user roles for users who doesn’t have any. (about 8000 users)
- Shold I manually add ‘cap’ to admin role ?
- Getting a user role from the user login name
- Why is wp-login redirecting to the home page when I use this function?
- Allow contributor to view own scheduled post
- How are roles stored in the database?
- I am adding a new class to my body tag if the logged in user is subscriber, need help
- How to Orderby User Role?
- The delete_posts capability?
- get approved users only ( ultimate member plugin )
- Create Super Admin that cant be deleted
- remove/hide pages from users backend
- Assign second role to user
- Adding Capabilities to a WordPress User Account
- New user is assigned 2 roles: customer and superadmin
- how to set any post creation for pending status and let the admin approve it first?
- Allowing user to edit posts based on the post status
- Allow user role “salesman” to view only “client” role users
- Purpose of Adding Capability to Role But Not Grant?
- bbPress: How to set conditional for specific user -or- user role, to be displayed in user profile
- Menu page with list of users
- How to check user role?
- Create a User Role with permissions to only upload files to the media library?
- Get and display a user’s profile info?
- How to get a users list by who created them?
- Paypal API and WordPress
- Is there a Capability that checks as true for non-logged in users?
- Building Forums with Custom Post Types
- remove_cap nothing changes
- get_post_meta with related user
- How to change a user’s role depending on date registered?
- Show special backend content for certain roles
- Why can my subscribers create new posts for review?
- Custom Role read_private_posts Not Working
- Saving user meta “member_id” based on user role
- Restrict user to edit only edit a certain menu
- Publishing post strips custom html element when user is not admin
- Allow multiple roles to specific user
- How do I restrict a second admin certain access?
- user has permission to read a custom post but it is not being shown in wordpress plugin
- Restrict custom fields based on user roles
- Step by Step Instructions for Making Media/Uploads Private to Only Logged-In Users
- Membership Plugin [closed]
- How to assign role to a custom registration form?
- Check if specific role exists
- Front-end ajax problem all users and guests getting a 302 redirect when accessing wp-admin/admin-ajax.php
- How to assign a custom gender to a user role?
- assign roles to users in custom drop down in signup form
- Hide block of HTML if the users role is a certain role
- How might I enable a user to view Draft pages from a different Author, without the ability to edit?
- Display users by role