WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_insert_user breaks if I set an ID (3 posts)

  1. Drew Baker
    Member
    Posted 1 year ago #

    Hey Guys,

    I've been playing around with wp_insert_user(). It seems to not work if I set an ID. If I leave that ID empty, then it will create the user just fine. The thing is, I really want to be able to set the ID (I want the new users ID to match their Facebook ID).

    Am I doing something wrong? Or do you think this is a bug?

    /*
          * Create WP
          * http://codex.wordpress.org/Function_Reference/wp_insert_user
          */
            add_action('wp', 'create_wp_user');
            function create_wp_user() {
                    $wp_user_data = array(
                        'ID' => 12345,
                        'user_pass' => wp_generate_password(),
                        'user_login' => $login,
                        'user_email' => $email,
                        'display_name' => $name,
                        'first_name' => $first_name,
                        'last_name' => $last_name,
                        'role' => get_option('default_role')
                    );
                    wp_insert_user($wp_user_data);
                }
            }
  2. Paul Clark
    Member
    Posted 1 year ago #

    The ID field is only used for updating existing users. Otherwise, IDs are assigned by the database auto-increment to prevent conflicts.

    For associating a Facebook ID with a user, you should use user meta or a user meta plugin.

    You can later search for users by their Facebook username using get_users with the meta_key and meta_value arguments.

    For example:

    <?php
    $args = array(
        'meta_key' => 'facebook_id',
        'meta_value' => '12345',
    );
    
    $users = get_users( $args );
    
    var_dump( $users );
    
    ?>
  3. Drew Baker
    Member
    Posted 1 year ago #

    Thanks, now that I think about it I think I'll map the FB ID to the WP username. They are both unique numbers, so it should be fine.

    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic