创建用户
使用wp_insert_user( $userdata )
函数创建用户,其中user_login和user_pass是必须项。
可能的错误包括以下几种:
- invalid_user_id
- empty_user_login
- user_login_too_long
- existing_user_login
- invalid_username
- user_nicename_too_long
- existing_user_email
判断创建用户是否成功则只需要判断if( !is_wp_error($user_id) )
。
设置当前用户
使用wp_set_current_user($id, $name = '')
函数设置当前用户,然后就可以通过get_currentuserinfo()获取信息。
但是注意,这是一个使用方便但不安全的接口:
Some WordPress functionality is based on the current user and not based on
the signed in user. Therefore, it opens the ability to edit and perform
actions on users who aren’t signed in.
校验账号密码是否正确
使用wp_authenticate_username_password($user, $username, $password)
函数校验账号密码是否正确,如果该