WP_User::set_role( string $role )
- Since
- 2.0.0
- Source
wp-includes/class-wp-user.php:609
Description
This will remove the previous roles of the user and assign the user the new one. You can set the role to an empty string and it will remove all of the roles from the user.
Compatibility
- WordPress
- since 2.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$rolestring- Role name.
Performance profile
How much work a call to WP_User::set_role() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 12–64
- Plugin surface
- 3 hooks
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 81.
Third-party callbacks on 'remove_user_role', 'add_user_role', 'set_user_role' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()called directly
Further down the call graph this can also reach serialize, cache, query, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_User::set_role() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$role === false | 12 | current() |
| always | 41–51 | update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action() |
$role !== false | 47–57 | current(), update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action() |
!$role | 51–58 | update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action(), do_action() |
$role !== false && !$role | 57–64 | current(), update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 81 | 12–64 | 11 | |
| 8.5 | 81 | 12–64 | 11 | |
| 8.4 | 81 | 12–64 | 11 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 84 | 12–67 | 11 | |
| 8.2 | 84 | 12–67 | 11 | |
| 8.1 | 84 | 12–67 | 11 | |
| 7.4 | 84 | 12–67 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 3
3 hooks fire while WP_User::set_role() runs, in this order:
- do_action( remove_user_role )actionline 637 (+28 into the body)
Fires immediately after a role as been removed from a user.
- do_action( add_user_role )actionline 642 (+33 into the body)
Fires immediately after the user has been given a new role.
- do_action( set_user_role )actionline 655 (+46 into the body)
Fires after the user's role has changed.
Uses · 4
- update_user_meta()Updates user meta field based on user ID.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_User::get_role_caps()Retrieves all of the capabilities of the user's roles, and merges them with individual user capabilities.
- WP_User::update_user_level_from_caps()Updates the maximum user level for the user.
Source code
public function set_role( $role ) { if ( 1 === count( $this->roles ) && current( $this->roles ) === $role ) { return; } foreach ( (array) $this->roles as $oldrole ) { unset( $this->caps[ $oldrole ] ); } $old_roles = $this->roles; if ( ! empty( $role ) ) { $this->caps[ $role ] = true; $this->roles = array( $role => true ); } else { $this->roles = array(); } update_user_meta( $this->ID, $this->cap_key, $this->caps ); $this->get_role_caps(); $this->update_user_level_from_caps(); foreach ( $old_roles as $old_role ) { if ( ! $old_role || $old_role === $role ) { continue; } /** This action is documented in wp-includes/class-wp-user.php */ do_action( 'remove_user_role', $this->ID, $old_role ); } if ( $role && ! in_array( $role, $old_roles, true ) ) { /** This action is documented in wp-includes/class-wp-user.php */ do_action( 'add_user_role', $this->ID, $role ); } /** * Fires after the user's role has changed. * * @since 2.9.0 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles. * * @param int $user_id The user ID. * @param string $role The new role. * @param string[] $old_roles An array of the user's previous roles. */ do_action( 'set_user_role', $this->ID, $role, $old_roles ); }Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wp-user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.