Because you are operating on the current (logged in) user – you need to accept parameters in your callback so that you can operate on the user whose role is being set:
rb_update_user_role( $user_id, $role ) {
// error_log( "Setting user $user_id to role $role" );
if ( $role !== 's2member_level0' && $role !== 's2member_level1' )
return;
$user = new WP_User( $user_id );
if ( $role === 's2member_level0' ) {
$user->remove_role( 'vendor' );
$user->add_role( 'customer' );
} else {
$user->remove_role( 'customer' );
$user->add_role( 'vendor' );
}
}
add_action( 'set_user_role', 'rb_update_user_role', 10, 2 );
Note you do not need to remove any roles – WP_User::set_role explicitly removes all other roles before it fires the action.
Related Posts:
- Apply custom role capabilities to administrator (without plugin)
- Roles & capabilities GUI that does not create separate table
- How do I redirect upon login a specific user based on role?
- How to add another user to this remove_menu function?
- Add a new subscriber role using a function
- How to test for Super Admin
- Custom roles can’t access to wp-admin
- Custom registration URL lost when user makes signup mistake
- Custom function for specific user role
- Hide Author.php template from specific user role
- edit role display name and label name without plugins
- create shortcode to list users with specific meta key value
- Toggle User Roles with button
- How to change or add user role after getting post request data about pay? [closed]
- Execute a ultimate member action when user role is updated
- Allowing users to view private posts (pending approval)
- Allow Contributor to edit their own posts after Adm aproval
- How to add custom JavaScript in functions?
- How to set default post editor based on role?
- load CSS only for administrators on backend
- Adding additional roles on registration
- The Capability to choose post/page template
- How to add array [closed]
- Get users with different roles and call function on each of them (user_meta)
- How can I show different content for different user-levels?
- Custom user role still showing up after deletion, ideas?
- How to load a css file depending on the current role
- Allow user select role in wordpress default registration without plugin
- Remove check boxes and its label from screen option for custom role
- Add Change role button for list of user in an event
- How to show only specific category post by user role without plugin and restrict all other cats
- Change user role based on total number of items ordered
- Remove all video attachment, both files and post/postmeta from database?
- delete_user_meta for user in spesific group
- Restrict certain roles registrations by domain
- Multisite Admin Roles
- add_role menu pages are not visible
- Welcome Mails based on user role
- Get all users from role and add to dropdown (select) – wordpress, javascript
- Would it be possible to disable embeds for specific user roles? If so, how?
- How to make my logged-in user-role shortcode displaying name instead of slug?
- 400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook
- How long does a deprecated function live in core?
- remove links from images using functions.php
- functions.php inject inline css
- How to add css class to cloud tag anchors?
- Removing custom background and header feature in child theme
- override parent theme configuration in child functions.php
- Function in Child Theme not overriding Parent Theme function [duplicate]
- Php string not working in WordPress Functions.php (trying to fetch 1st category for each blog that post appears in the sidebar)
- How to save Uploaded image in custom option panel?
- How to integrate owl carousel into theme without using a plugin?
- How to add an export function to a custom Option Theme Page
- WordPress function/template tag to get first n words of the content
- My function doesn’t work into WordPress but outside, it works – what am I missing?
- How to add iOS & fav icons to the theme?
- Defer all js except certain ones in functions.php file
- Getting RID of thickbox!
- Multisite, either change the name of sample-page or create new page
- What is the “icon” parameter in wp_get_attachment_image_src used for?
- How to use thumbnails in gallery?
- Customizer field value into functions.php variable
- How can I load a javascript file that is type=”module” the WordPress way?
- How to manage arrays from custom functions stored in functions.php?
- Running a script before absolutely everything
- Auto delete post if certain custom field data is empty
- using `require` in my functions.php breaks the site
- Is it OK to include files using ABSPATH?
- How to fetch the name of the active menu?
- Display gallery on bottom after content
- WordPress Shortcode function display outside of widget
- Storing password (functions.php)
- Check if the image size is available and if not use ‘full’ image size
- Display a random tag but using cron to control frequency of change
- How Do I include/Import a Custom Widget from the Parent theme into My Child Theme?
- Perform function on publish AND save (not just save)
- How to add or delete default value theme options while activate and deactivate theme
- Pass URL to iframe in WordPress Block
- How do I make the comment disappear after deleting from the database?
- External api call and make global variable for any page visitor enters , page-home, page, single etc
- WP AJAX Request Not Working
- Unable to login after registration
- Remove the ‘category’ url for one category type
- blank page with wp_get_attachment
- Add another role to a user when they click a button?
- Altering page / category names in breadcumbs
- Including admin-options.php file in Child Themes
- Load a Child Theme’s style.css just before the closing tag
- Can’t wrap my head around this function – any help would be appreciated
- Figure inlined width trigger distorted layout
- Exclude javascript in certain page url and all following url’s after forward slash
- Shortcode Variations?
- How to test for a class (from a plugin) in functions.php
- Child Theme not working – CSS gone
- Which template file to edit to edit homepage in Mystile [closed]
- How to use webpack in WordPress theme? I want some scripts to load in the footer, some in the header and some with script parameters
- How can I able to exclude ‘Category’ and ‘Tag’ slug from the URL in WordPress without reloading to new site?
- How to build an expiring function in WordPress?
- Function to filter numbers from string
- Is it possible to automatically set the publication time to 8am on new posts?