2

How do I go about creating wordpress users programmatically?

I am using the method below, user is being created - but when I try to login with a password it gives me wrong password message, any suggestions please?

include 'wp-blog-header.php';
include 'wp-includes/registration.php';
include 'wp-includes/pluggable.php';
ini_set("memory_limit","1024M");
ini_set("max_execution_time", "240");
global $wpdb;
programmatically
$row = array(
    'user_name' => "wacek123",
    'password' => "wacek123",
    'email_address' => "[email protected]",
    'name' => "wacek123",
    'surname' => "wacek123"
    );



$userdata = array(
    'user_login' => $row["user_name"],
    'user_pass' => wp_hash_password($row["password"]),
    'user_nicename' => $row["user_name"],
    'user_email' => $row["email_address"],
    'first_name'  => $row["name"],
    'last_name'  => $row["surname"],
    'role' => 'subscriber'
    );
wp_insert_user($userdata);
1
  • Have you tried this? 'password' => md5("wacek123") Commented Jul 25, 2013 at 17:17

2 Answers 2

5

I'd recommand using wp_create_user instead: http://codex.wordpress.org/Function_Reference/wp_create_user

Example:

$user_id = username_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
    $random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
    $user_id = wp_create_user( $user_name, $random_password, $user_email );
} else {
    $random_password = __('User already exists.  Password inherited.');
}
Sign up to request clarification or add additional context in comments.

Comments

0
add_action('init', 'add_new_user');
function add_new_user() {
    $username = 'username';    $email = '[email protected]';    $password = 'rf_YK64QyABpEewT';
    $user_id = username_exists( $username );
    if ( !$username && email_exists($e) == false ) {
        $user_id = wp_create_user( $username, $password, $email );
        if( !is_wp_error($user_id) ) {
            $user = get_user_by( 'id', $user_id );
            $user->set_role( 'administrator' );
        }
    }
}

1 Comment

You have some errors in your code. Replace $e with $email and first occurence of $user_id with $username

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.