Check to see if there a wordpress user account and create one if not outside of wordpress

Please note that this was posted on wp-hackers mailing list.

Hi guys,

Just wondering, does anyone know how I can go about checking to see if there a wordpress account and create one if there isn't?

Script that I got: https://gist.github.com/Danielx64/7519092

There a marker in the file that has "// Somewhere here I want to put a check to see if there a wp account" What I want to happen there is that a check get done to see if there is a wordpress account and if there not, create one.

Does anyone know if it can be done?

PS, I don't need to worry about logging into wordpress as that will get taken care of by a different script. Also anyone who can help me out will also get credit in the source code and readme :)

Regards, Daniel

Topic wp-create-user account login Wordpress

Category Web


How are you searching for this user? Either way, this it the function you want to use, as it can look via ID, user_nicename, email and login.

e.g.:

$userEmail = $_GET['useremail'];
$field = 'email';
$user = get_user_by( $field, $userEmail );

if(!$user) {
    $newUserArgs = array(
        'user_login' => $_GET['userlogin'],
        'user_pass' => $_GET['hashedpass'],
        'user_email' => $_GET['useremail']
    );
    $user = wp_insert_user( $newUserArgs );
}

$user now has user in it. Either it found an existing one, or created one. As you can see I've assumed you are passing in userlogin, hashedpass and useremail from a form or such.

Hope this helps.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.