1

I created a registration form where I integrate Stripe for payment.

Everything works correctly and I was also able to create the wp_users and wp_usermeta table in the database.

The only thing I can't understand is how I can assign a role.

This is the code I currently use:

//Sanitize POST Array
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);

$nome = $POST['nome'];
$cognome = $POST['cognome'];
$email = $POST['email'];
$token = $POST['stripeToken'];

$nickname = $nome . ' ' . $cognome;

// Inserisce utente nel DB
$user_data = [
    'user_login' => $nickname,
    'user_pass'  => wp_generate_password (),
    'user_email'   => $email,
];
$user_id = wp_insert_user($user_data);

update_user_meta( $user_id, 'first_name', $nome);
update_user_meta( $user_id, 'last_name', $cognome);
update_user_meta( $user_id, 'last_name', $cognome);

Now how can I assign a previously created role to the user?

RESOLVED:

I managed to solve by adding this code dopo l'ultima linea di codice sopra riportato:

$u = new WP_User( $user_id );
$u->remove_role( 'subscriber' );
$u->add_role( 'bronze_member' );

Sincerely, Feduzi Matteo.

0
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);

$nome = $POST['nome'];
$cognome = $POST['cognome'];
$email = $POST['email'];
$token = $POST['stripeToken'];

$nickname = $nome . ' ' . $cognome;

// Inserisce utente nel DB
$user_data = [
    'user_login' => $nickname,
    'user_pass'  => wp_generate_password (),
    'user_email'   => $email,
];
$user_id = wp_insert_user($user_data);

update_user_meta( $user_id, 'first_name', $nome);
update_user_meta( $user_id, 'last_name', $cognome);
update_user_meta( $user_id, 'last_name', $cognome);
wp_update_user( array ('ID' => $user_id, 'role' => 'editor') ) ;

Please try this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.