Come assegnare multipli Ruoli ad un Utente WordPress

Scopri come leggere la slug di un Ruolo di uno specifico Utente.

Se devi assegnare un singolo Ruolo ad un Utente (o rimpiazzare quello attuale), usa quest’altro metodo.

// get and print the role of the user with user_id = 66

function get_user_role($user_id) {

$user = get_userdata($user_id);

if ($user) {

$roles = $user->roles;

if (count($roles) > 0) {

return $roles;

}

}

return null;

}

function print_user_role($user_id) {

// echo the ID of the logged user

if(get_current_user_id() != 10)

return;

// customer, remo_gas, remo_luce

// set the roles of user_id = 66 to customer, remo_gas and remo_luce

$user = new WP_User(66);

$roles = array(

‘customer’,

‘remo_gas’,

‘remo_luce’

);

$user->add_role(‘customer’);

$user->add_role(‘remo_gas’);

$user->add_role(‘remo_luce’);

// get the roles of user_id = 66

$role = get_user_role(66);

if ($role) {

foreach($role as $r) {

echo $r;

}

}

}

add_action(‘wp_head’, ‘print_user_role’);