Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Programmatically Set the WordPress User Role
<?php
/**
* Sets the role for the user as specified in the function.
*
* @param int $user_id The ID of the user for whom we're updating the role.
*/
public function set_user_role( $user_id ) {
// Define a user role based on its index in the array.
$roles = array(
'administrator',
'editor',
'author',
'contributor',
'subscriber'
);
$role = $roles[2];
// Set the user's role (and implicitly remove the previous role).
$user = new \WP_User( $user_id );
$user->set_role( $role );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.