wppaste
WordPress

WP_User::set_role( string $role )

Since
2.0.0
Source
wp-includes/class-wp-user.php:620
Sets the role of the user.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–64

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 81.

Plugin surface
3 hooks

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.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls it makes
$role === false12current()
always41–51update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action()
$role !== false47–57current(), update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action()
!$role51–58update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action(), do_action()
$role !== false && !$role57–64current(), update_user_meta(), ->get_role_caps(), ->update_user_level_from_caps(), do_action(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8112–6411
8.58112–6411
8.48112–64113 fewer instructions than PHP 8.3
8.38412–6711
8.28412–6711
8.18412–6711
7.48412–6711

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:

  1. do_action( remove_user_role )actionline 648 (+28 into the body)

    Fires immediately after a role as been removed from a user.

  2. do_action( add_user_role )actionline 653 (+33 into the body)

    Fires immediately after the user has been given a new role.

  3. do_action( set_user_role )actionline 666 (+46 into the body)

    Fires after the user's role has changed.

Uses · 4

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.