can you tell me what's wrong with this code?
I simply have to enter those data in the user database on Wordpress.
Here is my code:
// Inserisce utente nel DB
$user_data = [
'user_login' => $nickname,
'user_pass' => wp_generate_password(),
'user_email' => $email,
];
$user_id = wp_insert_user($user_data);
// Inserisce i meta dati di un utente nel DB
update_user_meta( $user_id, 'first_name', $nome);
update_user_meta( $user_id, 'last_name', $cognome);
$u = new WP_User( $user_id );
$u->remove_role( 'subscriber' );
$u->add_role( 'bronze_member_ga' );
The only thing I added in the wpdd_users database structure is that CURRENT_TIMESTAMP
UPDATE:
The problem is not in the user's cache. Those work perfectly.
The problem is the username checks that I had not made.
I write to him and then post them.
can you tell me what's wrong with this code?
I simply have to enter those data in the user database on Wordpress.
Here is my code:
// Inserisce utente nel DB
$user_data = [
'user_login' => $nickname,
'user_pass' => wp_generate_password(),
'user_email' => $email,
];
$user_id = wp_insert_user($user_data);
// Inserisce i meta dati di un utente nel DB
update_user_meta( $user_id, 'first_name', $nome);
update_user_meta( $user_id, 'last_name', $cognome);
$u = new WP_User( $user_id );
$u->remove_role( 'subscriber' );
$u->add_role( 'bronze_member_ga' );
The only thing I added in the wpdd_users database structure is that CURRENT_TIMESTAMP
UPDATE:
The problem is not in the user's cache. Those work perfectly.
The problem is the username checks that I had not made.
I write to him and then post them.
There are a few things to note:
wp_insert_user
, so there's no way to know if it failed and whywp_insert_user
can be used to set the role etc, the follow up calls aren't needed, if you read the official docs, there are parameters for those fields So, lets do it this way:
$user_data = [
'user_login' => $nickname,
'user_pass' => wp_generate_password(),
'user_email' => $email,
'first_name' => $nome,
'last_name' => $cognome,
'role' => 'bronze_member_ga'
];
$user_id = wp_insert_user($user_data);
Then confirm that it actually created the user, and if not, why it failed:
//On failure
if( is_wp_error( $user_id ) ) {
wp_die( "Failed to create user: " . $user_id->get_error_message() );
}
Update: Based on your responses it would appear that several assumptions you've made are untrue, specifically that $nickname
has a value and is valid
So lets add a validation and sanitisation step before filling $user_data
:
if ( empty( $nickname ) ) {
wp_die( 'Failure: $nickname is empty and must have a value' );
}
if ( !is_email( $email ) ) {
wp_die( 'Failure: $email is not an email' );
}
if ( empty( $nome ) ) {
wp_die( 'Failure: $nome is empty and must have a value' );
}
if ( empty( $cognome ) ) {
wp_die( 'Failure: $cognome is empty and must have a value' );
}
For your database changes, you should instead make use of update_user_meta
and get_user_meta
to store the additional values. Using extra columns in the user table will cause problems, and data loss on WordPress updates
$user_id
might be aWP_Error
object – Tom J Nowell ♦ Commented Aug 29, 2019 at 14:59