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 do I make a draft post accessible to everyone?
- Allow roles below admin to add subscribers only
- Disallowing Users of a Custom Role from Deleting or Adding Administrators?
- Hide Admin Menu for Specific User ID who has administrator Role
- How to force listing contributors in authors dropdown menu
- How do I allow a specific Role in WordPress 3+ to only see certain plugins?
- Allow editors access to added plugins
- Change role after the user has read all the posts in a category
- Do custom user roles have any default capabilities?
- Can i copy administrator role to another user?
- How to prevent users with “edit_others_posts” capability from editing admin posts
- How to let someone preview a draft?
- How can I allow user to select minimum privilege smartly
- Remove wordpress author’s capability to moderate comments on their own posts
- Contributor disable seeing others’ posts
- access the plugins for each role in WordPress multisite
- Listing child pages depending on user capability (role)
- How to show a gloabl message on a user profile page (in back end)?
- How do I display user name, role and site name using HTML tags inside a dashboard notification?
- Displaying different in-page content to cliente/admin
- User Role Capabilities for WordPress Gutenberg Blocks
- Iframe disappears when author updates page
- How to change a user’s role conditionally to expire after two weeks
- Stop users of author role from editing already pending posts
- how to remove some permissions from a shop “manager role” in woocommmerce?
- reset the users roles – is there a way to do this?
- Custom wordpress admin page/url “You do not have sufficient permissions to access this page.”
- user_can() not working for comment authors
- WordPress add_rewrite_rule redirection match GET variable not passing through to custom template
- Redirect admin 403 “Cheatin uh?” admin pages
- Migrating Roles from one environment to another
- Letting users create a post that is a custom post type from a page
- Is WordPress secure enough for a multi-user article directory?
- Learndash change user role after completing the course
- How to disable activation email to specific user role?
- logout users with specific role after close browser tab
- Admin user name not showing up in author dropdown
- Restrict admin pages for specific user role
- Separate user bases or hide users of another role or connected to another minisite
- How to restrict subscriber editing other posts but read specific posts in backend
- Change role in maintenance mode
- Allowing custom role user to edit post assigned to them but don’t let them create new custom type post
- How to make this custom capability work? [duplicate]
- Give a user role capability to create orders for clients
- Restrict content piece by role – Protection message in content?
- WordPress install checking permissions of user id 0
- Force “submit for review” on update?
- Allow users to change author of post
- Custom User Role
- Is there a way for a limited user to just view the page of a post without being able to edit it?
- How do I make a draft post accessible to everyone?
- Authors Ability to Review Post from Contributors
- Control Category of each user can post
- User role can’t be set
- Order Status based on User Role
- Why is user_can_access_admin_page() an undefined function?