Use the following function:
function wpse_replace_user_role( $from, $to ) {
$args = array( 'role' => $from );
$users = new WP_User_Query( $args );
if ( !empty( $users->get_results() ) ) {
foreach( $users->get_results() as $user ) {
$u = new WP_User( $user->ID );
$u->remove_role( $from );
$u->add_role( $to );
unset( $u );
}
unset( $users );
}
}
- $from – query all users with this role
- $to – role to be set
instead, for each queried user
Related Posts:
- How to create a clone role in wordpress
- How do I make a draft post accessible to everyone?
- Create custom user role (client) that can create another custom user role (employee) of that client
- Allow roles below admin to add subscribers only
- How to force listing contributors in authors dropdown menu
- Allow authors to edit only certain users
- How do I allow a specific Role in WordPress 3+ to only see certain plugins?
- Allow editors access to added plugins
- Temporarily disable user role login and replace with message
- Change role after the user has read all the posts in a category
- Do custom user roles have any default capabilities?
- Filter available WooCommerce payment gateways by role [closed]
- How to programmatically add a user to a role?
- Can i copy administrator role to another user?
- I need to assign a role to visitors/guests
- How to make an author archive only for certain user role and show related CPT
- Allow users to publish child pages of the pages they have access to edit
- Is there a way to set the user Role based on email domain
- pre_get_posts Remove posts based on meta value with ‘post__not_in’
- Access on specific pages in wordpress for a specific user
- How to get all users with Author role capabilities?
- Change the user role after x days
- Get (echo) all role names assigned to user
- Add Role inherits?
- similar to Editor can create any new user except administrator
- How to check if a role has a specific capability
- Expire a user’s secondary role after X days from it being allocated
- Locking Down WordPress Application Password Permissions / Capabilities
- Can a user with admin role get ‘Sorry, you are not allowed to move this item to the Trash’ error when trashing a post?
- Making shortcode of filtered number of comments by user role
- How to stop contributors editing post type but allowing them to edit a custom post type?
- Allow Block Editing By User Role
- WordPress missing user roles on local dev machine. Live site works fine
- Programmatically allow a non-author to edit a post based custom meta field
- Are there individual memory allocations for different user roles in WordPress?
- How to redirect specific post type with user role
- Prevent Editors from Editing/Deleting Admin Accounts
- Membership subscription, change user role when subscription runs out
- Pending status by default for a specific role
- Getting a role based on a localized role name
- How do I restrict user access to plugins?
- Change user role after bulk-import
- Show metabox for a special role
- Admin User Role Unable to See Private Posts
- Custom capabilities to add, edit, remove users of a particular role only?
- Undo User Role Name Change
- add_role user capability not working
- Limit a user to have access to only specified pages?
- How can I promote a user to a network administrator?
- read_private_pages capability not working for new role
- Restrict custom post content to specific user
- List users of an array of roles
- How can I programattically hide all admin notices for everyone except admin users
- How to exclude subscriber from the list in wordpress
- Want to know parameters that can be passed to user_can() for access control by user capabilities
- Block user roles from accessing the WordPress dashboard
- Create sub-administrator role that can do everything except use or see the code editor
- Can we hook the Customizer on a post or page on the front of the site?
- display user roles in comment form
- Is it proper to build a site supporting blog visitors in WordPress?
- Removing capabilities from cloned admin role
- User role editor – Add download files capability
- Setting an expiration date for wordpress roles
- Could we know who published a post on WordPress?
- Auto Change WordPress Roles
- Error while creating a clone role in wordpress
- Custom Plugin – Blacklist/Whitelist domain email and change user role – Suggestions
- Change auth_cookie_expiration for specific roles and specific login times
- Infinite redirects at front end if logged in user is not an Admin (Toolset Access)
- Set user role, if an specific role created an user
- Block access to plugins.php for custom role
- Allow user to select role from front end edit profile form
- How to make WP page accessile only to specific user roles
- Remove add post for user role
- Restrict Author to post only to a specific category
- WordPress – Limit custom/specific user role to only have access to edit the contents of an Advanced Custom Fields Pro Options Page
- TCPDF only exports .pdf when user is administrator
- How to allow suscriber to access specific pages in dashboard?
- Enable plugins for a specific user role
- Switch role on submit button
- Custom User Role – can’t edit or publish pages?
- Dropdown of user roles that depend on variable
- Add role selector to custom registration page
- Multiple user role on registration
- Trying to get new role to have edit permissions for custom content type
- Why are all roles assigned to a user when using wp_insert_user()?
- Why does user_can return false for a capability during plugin deactivation?
- Removing Admin Bar Node Based on Role
- Create an email address that forwards to users in role
- How to update user role totals
- Custom user role doesn’t show up in admin
- Welcome email specific to user role
- update_user_meta on registration but only for default role type
- New folder and file permissions are not correct
- How to change user role setting in members plugin so that user can only edit his own post?
- 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