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

enter image description here


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.

  • 1
    Can you clarify what's going wrong? Is the user inserted but not the meta? – butlerblog Aug 29 '19 at 14:55
  • I checked and I see this: The $ userdata array is created correctly and if I print it with a print_r ($ userdata) all the entered data is shown. But when I go to add them via $ user_id = wp_insert_user ($ user_data); these are not entered. Consequently, of course, even usermetas are not inserted. – Matteo Feduzi Aug 29 '19 at 14:58
  • So a user is crated but the user meta is not? Or is no user created at all? It's unclear what's wrong, I also see you don't check for errors in your code, $user_id might be a WP_Error object – Tom J Nowell Aug 29 '19 at 14:59
  • Neither was created. – Matteo Feduzi Aug 29 '19 at 15:00
  • Maybe it can be a value that I changed in the database? It seems to me that it no longer records the data from that change if I'm not mistaken. – Matteo Feduzi Aug 29 '19 at 15:01

There are a few things to note:

  • there is no error checking on wp_insert_user, so there's no way to know if it failed and why
  • wp_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
  • Never modify the WP Core database tables yourself, if you need extra columns, store the data in user meta, any changes you make get undone and overwritten when WP updates its database table schema, all that extra data would be lost

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

  • I have placed the database to the previous version but nothing has been solved. I followed your instructions and I see this error. Ideas of what it means? Failed to create user: Unable to create a user without a login name. – Matteo Feduzi Aug 29 '19 at 15:28
  • While the user_login is passed correctly. – Matteo Feduzi Aug 29 '19 at 15:29
  • @MatteoFeduzi are you absolutely sure that the value is valid? What are you using to test? And are you absolutely sure it is in the $user_data array? How are you checking that? Where does $nickname come from? – Tom J Nowell Aug 29 '19 at 15:35
  • Unfortunately I haven't solved it yet, I thought it was that but it's not ... – Matteo Feduzi Aug 29 '19 at 15:50
  • I think the problem is due to the caches. In fact at the bottom of the usage file: wp_set_current_user ($ user_id); wp_set_auth_cookie ($ user_id); wp_redirect (home_url (). '/ dashboard'); exit; – Matteo Feduzi Aug 29 '19 at 15:53

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.