0

Below insert of new user does not work. I have no idea why not. There are no error messages send to debug.txt or to screen. I have also checked a bunch of thread about this insert. But it seems my code should be correct. But not working.

What do I do wrong in this code?

All input fields are correct filled.

if($loggedin == 'no'){

    $userdata = array(
'user_login'        =>  $user_email,
'user_pass'         =>  $pass,
'user_email'        =>  $user_email,
'display_name'      =>  $first_name . ' ' . $last_name,
'nickname'          =>  $first_name . ' ' . $last_name,
'first_name'        =>  $first_name,
'last_name'         =>  $last_name,     
'user_registered' =>    date('Y-m-d h:i:s'),
    );
    $user_id = wp_insert_user( $userdata ) ;

if( is_wp_error( $user_id  ) ) {
echo $return->get_error_message();
}

    if($user_id && $user_id != 0){                  
        update_user_meta( $user_id, 'gender', $_POST['gender']);
        update_user_meta( $user_id, 'city', $_POST['city']);
        update_user_meta( $user_id, 'age',  $_POST['age']);
        update_user_meta( $user_id, 'bank_account', 0);
        update_user_meta( $user_id, 'user_status', 0);

        if(!is_admin() ) {
            wp_update_user( array ('ID' => $user_id, 'role' => 'participant' ) ) ;
            $user = new WP_User( $user_id );
            $user->set_role('participants');
        }

        $info = array();
        $info['user_login'] = $user_email;
        $info['user_password'] = $pass;
        $info['remember'] = true;

        $user_signon = wp_signon( $info, false );
    }

    echo 'registered';
}

Instead of insert I also try update. So disabled whole insert block and update that part with this below. Again no new user is added!

    $user_id = wp_create_user( $user_email, $pass, $user_email );

$wpdb->update( 
    $wpdb->prefix.'users', 
    array( 
        'ID'                            =>  $user_id,
        'display_name'      =>  $first_name . ' ' . $last_name,
        'nickname'              =>  $first_name . ' ' . $last_name,
        'first_name'            =>  $first_name,
        'last_name'             =>  $last_name,     
        'user_registered' =>    date('Y-m-d h:i:s'),            
    ), 
    array( 'ID' => $user_id ), 
    array( '%d', '%s', '%s', '%s', '%s', '%s' ), 
    array( '%d' ) 
);
1

I don't see where $return is being set. Try changing

if( is_wp_error( $user_id  ) ) {
    echo $return->get_error_message();
}

to

if ( is_wp_error( $user_id  ) ) {
    echo $user_id->get_error_message();
}

...and that should hopefully display any error messages that are generated.

  • no it did not display any kind of errors – jtermaat Sep 19 '17 at 15:13
  • Does var_dump( $user_id ); show you anything useful? – Pat J Sep 19 '17 at 15:58
  • nothing at all. Can I write directly to a file when doing the var_dump? – jtermaat Sep 19 '17 at 16:28
  • See code at start. Also try $wpdb->update() – jtermaat Sep 19 '17 at 16:45
  • 1
    I forgot to close if($loggedin == 'no') with if($loggedin == 'no'){ }. That causes this issue. Thanks for replying to this thread. – jtermaat Sep 21 '17 at 8:58

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.