0

I'm using Wordpress 4.5.3, when I call wp_insert_user, it works, user gets created normally, but it does not return the ID or even an error.

Test.php

$userdata = array(
    'user_login' => esc_attr($this->username),
    'user_email' => esc_attr($this->email),
    'user_pass' => esc_attr($this->password),
    'first_name' => esc_attr($this->first_name),
    'display_name' => esc_attr($this->tagline)
);
echo "Test 1";
$register_user = wp_insert_user($userdata);
echo "Test 2"; //This does not echo. Script stops running?

I then opened wp-includes/user.php and edited this:

$user = new WP_User( $user_id );

$meta = apply_filters( 'insert_user_meta', $meta, $user, $update );

foreach ( $meta as $key => $value ) {
    update_user_meta( $user_id, $key, $value );
}

foreach ( wp_get_user_contact_methods( $user ) as $key => $value ) {
    if ( isset( $userdata[ $key ] ) ) {
        update_user_meta( $user_id, $key, $userdata[ $key ] );
    }
}

if ( isset( $userdata['role'] ) ) {
    $user->set_role( $userdata['role'] );
} elseif ( ! $update ) {
    $user->set_role(get_option('default_role'));
}
wp_cache_delete( $user_id, 'users' );
wp_cache_delete( $user_login, 'userlogins' );

if ( $update ) {
    do_action( 'profile_update', $user_id, $old_user_data );
} else {
    return $user_id; // I ADDED THIS FOR TEST PURPOSES. IT WORKS, IT RETURNS THE ID
    do_action( 'user_register', $user_id );
}

return $user_id; // THIS IS WHERE IT SHOULD RETURN THE ID

I made it work editing users.php, but I didn't want to mess with the core to avoid problems when updating my Wordpress site.

Can anyone confirm if this is a bug, or I'm doing something wrong?

2
  • What php error do you get when it halts? Commented Aug 3, 2016 at 19:08
  • There's no PHP error at all, but I managed to solve it - I added it as an answer for future searches. Commented Aug 3, 2016 at 19:10

1 Answer 1

1

I found out what it was.

I had an action hook for user_register on my functions.php. This action hook would automatically login the user after registration. After removing it, it worked.

As requested, the hook was interfearing because I was already logged in. The hook would use wp_set_current_user($user_id); wp_set_auth_cookie($user_id); to log in after registration.

2
  • It would be useful to others if you elaborated a bit on why and how had your hook interfered with the function.
    – Rarst
    Commented Aug 3, 2016 at 21:05
  • All right, there it is Commented Aug 3, 2016 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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