$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp api - wp_insert_user if user exists|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp api - wp_insert_user if user exists

matteradmin11PV0评论

I have been at this for almost two hours, i'm sure i'm close, but can't find the nudge to make it work. I built a module from scratch to import users from a csv file, the user creation is working fine, and i'm happy with that.

For the process to be convenient, i want to be able to update the users too, and always work with the same csv file. The wp_update_user does nothing, no error, but values from array are not updated. update_user_meta does not work either. i suppose it is the way i return the $user_id, since the ID is not included in the array...

         $userdata = array(      
            'user_login' => $username,
            'nickname' => $username,
            'user_nicename' => $user_nicename,
            'user_email' => $user_email,
            'first_name' => $prenom,
            'last_name' => $nom,
            'user_registered' => date( 'Y-m-d H:i:s' ),
            'display_name' => $username,
            'show_admin_bar_front' => false,
            'role' => 'subscriber',
            'admin_color' => "fresh",
            'rich_editing' => "true", 
            );

        // check if user exists
        if (username_exists($username)) {
            $user_id = $userdata[0]->ID;
            $user_id = wp_update_user( $userdata );
            update_user_meta( $user_id, 'region', $region );
        } else {
        // create new user
            $user_id = wp_insert_user($userdata);
            wp_new_user_notification( $user_id, null, 'both' );
            update_user_meta( $user_id, 'region', $region );
        }

Edit : So here is the fix, in case anyone is looking for a similar issue :

    if (username_exists($username)) {
        $the_user = get_user_by('login', $username);
        $user_id = $the_user->ID;

       if (!is_wp_error( $user_id )){
           $push_id = array('ID' => $user_id);
           $merge = array_merge($userdata, $push_id);
           $user_id = wp_update_user( $merge );
           update_user_meta( $user_id, 'region', $region );
       }
       else {                                       
       $html_update = "Broken";
       }
     } else {
     // create new user
        $user_id = wp_insert_user($userdata);
        wp_new_user_notification( $user_id, null, 'both' );
        update_user_meta( $user_id, 'region', $region );
     }

Thank you birgire, it helped me find a solution

I have been at this for almost two hours, i'm sure i'm close, but can't find the nudge to make it work. I built a module from scratch to import users from a csv file, the user creation is working fine, and i'm happy with that.

For the process to be convenient, i want to be able to update the users too, and always work with the same csv file. The wp_update_user does nothing, no error, but values from array are not updated. update_user_meta does not work either. i suppose it is the way i return the $user_id, since the ID is not included in the array...

         $userdata = array(      
            'user_login' => $username,
            'nickname' => $username,
            'user_nicename' => $user_nicename,
            'user_email' => $user_email,
            'first_name' => $prenom,
            'last_name' => $nom,
            'user_registered' => date( 'Y-m-d H:i:s' ),
            'display_name' => $username,
            'show_admin_bar_front' => false,
            'role' => 'subscriber',
            'admin_color' => "fresh",
            'rich_editing' => "true", 
            );

        // check if user exists
        if (username_exists($username)) {
            $user_id = $userdata[0]->ID;
            $user_id = wp_update_user( $userdata );
            update_user_meta( $user_id, 'region', $region );
        } else {
        // create new user
            $user_id = wp_insert_user($userdata);
            wp_new_user_notification( $user_id, null, 'both' );
            update_user_meta( $user_id, 'region', $region );
        }

Edit : So here is the fix, in case anyone is looking for a similar issue :

    if (username_exists($username)) {
        $the_user = get_user_by('login', $username);
        $user_id = $the_user->ID;

       if (!is_wp_error( $user_id )){
           $push_id = array('ID' => $user_id);
           $merge = array_merge($userdata, $push_id);
           $user_id = wp_update_user( $merge );
           update_user_meta( $user_id, 'region', $region );
       }
       else {                                       
       $html_update = "Broken";
       }
     } else {
     // create new user
        $user_id = wp_insert_user($userdata);
        wp_new_user_notification( $user_id, null, 'both' );
        update_user_meta( $user_id, 'region', $region );
     }

Thank you birgire, it helped me find a solution

Share Improve this question edited Jan 27, 2019 at 17:50 Xavier C. asked Jan 27, 2019 at 15:59 Xavier C.Xavier C. 577 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When updating a user with:

$user_id = wp_update_user( $userdata );

you need to have the user ID set within $userdata.

Also make sure to validate the $user_id output of wp_update_user() as it can be either an integer or an instance of WP_Error, before using it in update_user_meta().

One can use is_wp_error( $user_id ) to check if $user_id is an instance of the WP_Error class.

PS:

Make sure to have a look at PHP errors while developing.

When the new fatal error protection (WSOD) kicks in after 5.1, one might need to disable it by defining the constant:

WP_DISABLE_FATAL_ERROR_HANDLER 

as true in wp-config.php on dev installs.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far