W3cubDocs

/WordPress

WP_User::add_role( string $role )

Add role to user.

Description

Updates the user’s meta data option with capabilities and roles.

Parameters

$role

(string) (Required) Role name.

Source

File: wp-includes/class-wp-user.php

public function add_role( $role ) {
		if ( empty( $role ) ) {
			return;
		}

		$this->caps[ $role ] = true;
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
		$this->get_role_caps();
		$this->update_user_level_from_caps();

		/**
		 * Fires immediately after the user has been given a new role.
		 *
		 * @since 4.3.0
		 *
		 * @param int    $user_id The user ID.
		 * @param string $role    The new role.
		 */
		do_action( 'add_user_role', $this->ID, $role );
	}

Changelog

Version Description
2.0.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_user/add_role