This quick how-to guide will show how to create a WordPress user after a form was submitted.
The following code needs to be placed in the (child-)theme’s functions.php file. In the registration form, the fields have to be named username, password, firstname and lastname.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php function ezfc_insert_user($insert_id, $total, $user_mail, $id, $output_data, $submission_data) { // uncomment the next line if you want to check for a single form only //if ($submission_data["form_id"] != 1) return; // do not create a user if already logged in if (is_user_logged_in()) return; $username = sanitize_user($submission_data["submission_elements_values_raw"]["username"]); $password = $submission_data["submission_elements_values_raw"]["password"]; $user_mail = sanitize_email($user_mail); $user_id = wp_create_user($username, $password, $user_mail); if (!is_wp_error($user_id)) { // update user meta update_user_meta($user_id, "first_name", $submission_data["submission_elements_values_raw"]["firstname"]); update_user_meta($user_id, "last_name", $submission_data["submission_elements_values_raw"]["lastname"]); } else { // error } } add_action("ezfc_after_submission", "ezfc_insert_user", 10, 6); |