Home WordpressWordPress cơ bản Tạo user trong wordpress

Tạo user trong wordpress

by admincp

Thêm người dùng vào CSDL:
Cú pháp: wp_insert_user($userdata);

$userdata: mảng dữ liệu user, stdClass hoặc đối tượng lớp WP_User.

$website = "http://codechutney.com"
$userdata = array(
    'user_login'    =>  'login_name',
    'user_nicename'  =>'Mr Hoang',
    'display_name'  => 'display name',
    'user_url'  => $website,
    'user_email' =>'[email protected]',
    'user_pass'=>wp_hash_password('123456')
);

$user_id = wp_insert_user( $userdata ) ;

//On success
if( !is_wp_error($user_id) ) {
 echo "User created : ". $user_id;
}

Bạn cũng có thể thay đổi mật khẩu sau khi tạo người dùng, với hàm sau:

wp_set_password('plaintext new user password',$user_id);

Biến $userdata chứa các trường thông tin dưới đây:
nickname,fist_name,last_name,discription,role,jabber,aim,yim..

Chi tiết:

The $userdata array can contain the following fields
Field Name Description Associated Filter
ID An integer that will be used for updating an existing user. (none)
user_pass A string that contains the plain text password for the user. pre_user_pass
user_login A string that contains the user’s username for logging in. pre_user_login
user_nicename A string that contains a URL-friendly name for the user. The default is the user’s username. pre_user_nicename
user_url A string containing the user’s URL for the user’s web site. pre_user_url
user_email A string containing the user’s email address. pre_user_email
display_name A string that will be shown on the site. Defaults to user’s username. It is likely that you will want to change this, for both appearance and security through obscurity (that is if you dont use and delete the default admin user). pre_user_display_name
nickname The user’s nickname, defaults to the user’s username. pre_user_nickname
first_name The user’s first name. pre_user_first_name
last_name The user’s last name. pre_user_last_name
description A string containing content about the user. pre_user_description
rich_editing A string for whether to enable the rich editor or not. False if not empty. (none)
user_registered The date the user registered. Format is Y-m-d H:i:s. (none)
role A string used to set the user’s role. (none)
jabber User’s Jabber account. (none)
aim User’s AOL IM account. (none)
yim User’s Yahoo IM account. (none)

Nếu không tồn tại ID, một user mới sẽ được tạo. Nếu bạn truyền giá trị ID hàm sẽ cập nhật dữ liệu cho người dùng đã tồn tại trong WordPress.

Để nhận được bài viết mới vui lòng đăng ký kênh kiến thức WordPress từ A-Z ở Form bên dưới. Bạn cũng có thể nhận được sự trợ giúp trên TwitterFacebook

Liên hệ

Công ty chuyên Thiết kế website uy tín nhất Miền Bắc: http://vinastar.net

Hotline tư vấn: 0989 48 3456

Nguồn: Sưu tầm trên internet

You may also like

Leave a Comment