2

如何以编程方式创建 wordpress 用户?

我正在使用下面的方法,正在创建用户-但是当我尝试使用密码登录时,它给了我错误的密码信息,请问有什么建议吗?

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' => "wacek123@gmail.com",
    '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);
4

2 回答 2

5

我建议wp_create_user改用: http ://codex.wordpress.org/Function_Reference/wp_create_user

例子:

$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.');
}
于 2013-09-24T12:14:46.513 回答
0
add_action('init', 'add_new_user');
function add_new_user() {
    $username = 'username';    $email = 'asdasd@asd.com';    $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' );
        }
    }
}
于 2020-09-23T15:24:01.437 回答