One step is to use the
editable_roles
filter to remove roles from the dropdown but this doesn’t prevent the user from modifying the select value and create a user with “not allowed” role.
Yes it does. This filter is not just for the dropdown. Modifying editable_roles
does in fact prevent users from assigning a role they’re not allowed to.
This is because edit_user()
(the function used for adding new users) calls get_editable_roles()
as well and bails when one is not allowed to give users that role.
Here’s a simple example of what you can do:
/**
* Removes Administrator from roles list if user isn't an admin themselves.
*
* This way, only admins can make new admins.
*
* @param array $all_roles List of roles.
* @return array Modified list of roles.
*/
function wpse_293133_filter_editable_roles( $all_roles ) {
if ( ! is_super_admin( get_current_user_id() ) ) {
unset( $all_roles['administrator'] );
}
return $all_roles;
}
add_filter( 'editable_roles', 'wpse_293133_filter_editable_roles' );
Related Posts:
- A different role for each site in a multisite
- WPMU – new users are automatically subscribed to the main blog – how to prevent that?
- How to change a user role after registering in multisite?
- Easily adding multiple existing users to a multisite site
- Why are my roles not visible in a Multi-site/Network?
- wordpress multisite, how to keep user on subdomain throughout registration process?
- How to enable a site administrator to edit users in a WordPress network/ multisite setup?
- Possible to make custom role in multisite that can add sites?
- How to add multiple existing users to a multisite site?
- In MultiSite, can some users automatically have Site Admin rights on all sites, without granting them Network Admin access?
- create_users capabilities on a role on multisite
- Update User Role Across Network when Main Site User is Updated
- Assign role to user on first login, if they are first (after admin)
- Get the User ID Who Owns a Given Blog ID in Multisite
- Make a user administrator to a sub directory site and a contributor to main site in multisite network
- Displaying a message upon user registration
- How to change user starting role in WordPress MultiSite?
- Allow Author on Site A capability to upload files on Site B in Multi Site
- Add a user to a specific blog when they register?
- Add menu items/actions for multisite users who are registered on the network but do not have a role or capability in any sites
- Four columns in the wp_users table
- Subsite access without being a member of the subsite in wp multisite network
- Create Custom Multisite User Role to Reduce Capabilities
- Restrict Capability of Administrator to Create, Edit and Delete Pages in Multisite
- Copy user role on multisite so the user can access subsites with same role
- How to fix that new users show up again in subsite of a Multisite?
- WordPress Multisite Add User
- can’t create user without email for an author after converting single site to multisite
- Possible to have duplicate usernames on different two multisites
- Modify new user email notification in network admin screen wp multisite
- Users getting linked unwanted to main mu in WordPress multisite (WPMU)
- Can’t activate a user on multisite install
- Multisite “Skip Confirmation Email” Log Out Problem
- How to create child/sub user under parent user
- How create a multisite setup with “phantom” accounts and passwords?
- Is there any way to give all users access to one blog in a multisite network without using a plugin?
- How to give “author” user role appropriate capabilities to add PollDaddy polls? WordPress multisite
- Using a number for limiting registering or banning on multisite
- Non-super-admin users cannot access CPT even though I have explicitly added the capabilities to the user role
- Getting a List of Currently Available Roles on a WordPress Site?
- Editor can create any new user except administrator
- How To Add Custom Form Fields To The User Profile Page?
- Where are available Roles Defined in the wp_ database?
- How to allow an user role to create a new user under a role which lower than his level only?
- Site admin in a network install can’t edit users?
- Remove Ability for Other Users to View Administrator in User List?
- How to use same email for multiple users
- Where can I find documentation on what characters are allowed in user names and why?
- How can I un-reserve a pending username registration?
- How do I programmatically set default role for new users?
- Is there a is_user_logged_in() for multisite?
- Groups of capabilities: users with multiple roles?
- User-edit role setting distinct from wp_capabilities? [closed]
- WordPress Multisite allow site admin to add user without email confirmation
- How to let contributors to create a new revision(draft) editing their published posts
- How to allow mixed case characters in multisite site name?
- Multisite – each site with it’s own set of users
- User Roles in multisite – odd behavior
- Multisite – site user limited only for this site
- WordPress single sign on using cookies with shared user role functionality between more than 2 wordpress subdomains
- User / membership Plugin [closed]
- WordPress Multisite Network Subdomain
- Importing Posts into New Website with Same User ID’s
- WordPress automatic Login on other page?
- Multisite: How to bypass wpmu_signup_user_notification and add my own notification logic?
- Groups roles & capabilities
- Allow users to register on multisite through WooCommerce using the same email address
- Multisite error when adding a user: already a member of this site
- Activation of new Registered site fails on multisite
- Is Multisite the RIGHT option for my case?
- Subscriber role – blank page
- Should I use MultiSite for a subdomain based wp site?
- User registration problem on multisites web
- Get current user outside of WordPress Multisite
- Multisite – User creation for second site from first site?
- How to Create WPMu New User?
- Multisite vs Role Scoper
- WordPress MU users – how are they organized?
- Get users from all/specific blog by user_role and current_user role
- Possible to set new user’s site time zone at user creation using Gravity Forms?
- multisite registration: check existing subdomains while typing
- How can I display all Multisite blogs where this user is administrator?
- Redirect authors from upload.php url to Home page in Multisite
- WordPress multi user registration sites
- Registration Page
- Multisite and users being listed on network administration dashboard and not main site dashboard
- Want to add post to user dashboard
- How to use same email for multiple users? [duplicate]
- file upload user profile
- Unsure of my options, Multi-blog?
- WordPress Multisite (Network) some site users added to main site users list as subscriber
- WordPress Multisite restict user access
- current_user_can() returning true for capability when the user and role do not have the capability
- Multisite Login Access Restrictions
- Change a subsite Admin role of a WordPress Multisite after 24 hours registering
- How to use 2 different databases but share the same user in wordpress
- How to add default folders to Every registered User when registered in WordPress site?
- Condionally/limited emails from system (cantact-form, registration, passwd reset)With
- How can I get all capabilities for a particular user?
- Allowing a CPT post to be edited by a single user role