In this article, we will learn to create a new WordPress user with custom PHP coding.
To create the user “wp_insert_user()” function will be used.
Fetch the below values from the given form or data to create the new user.
$email = $_POST['email']; $password = $_POST['password']; $first_name = $_POST['name']; $last_name = $_POST['name'];
Pass the above data and create one array for the insert user function.
$user_data = array( 'user_login' => $email, 'user_email' => $email, 'user_pass' => $password, 'first_name' => $first_name, 'last_name' => $last_name, 'nickname' => $first_name, ); $user_id = wp_insert_user( $user_data );
After this code’s execution, a new user will be created in the system.
To log in the user right after the registration “wp_signon()” function will be used.
if($user_id){ $info = array(); $info['user_login'] = $email; $info['user_password'] = $password; $user_signon = wp_signon( $info, false ); echo "Register Done"; }
After this code’s execution, a new user will be logged in successfully to the system.
If you have any queries about this code snippet kindly mention them in the comments.