Enter user registration information in the database

admin2025-04-21  1

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.

Share Improve this question edited Aug 29, 2019 at 16:32 Matteo Feduzi asked Aug 29, 2019 at 14:40 Matteo FeduziMatteo Feduzi 291 silver badge9 bronze badges 10
  • 1 Can you clarify what's going wrong? Is the user inserted but not the meta? – butlerblog Commented Aug 29, 2019 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 Commented Aug 29, 2019 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 Commented Aug 29, 2019 at 14:59
  • Neither was created. – Matteo Feduzi Commented Aug 29, 2019 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 Commented Aug 29, 2019 at 15:01
 |  Show 5 more comments

1 Answer 1

Reset to default 2

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745201763a290096.html

最新回复(0)