This guide assumes you have a username, password and email address elements in your form. If you don’t you can modify the code in the examples below to suit your needs.
Add the following code to the theme functions.php file (or create a plugin for it). The first block of code checks that the given username does not exist already and returns an error if it does. The second block of code checks that the email address isn’t already registered. The third block adds the user to WordPress.
12 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 1920212223 24 25 26 27 28 29 30 31 | add_filter('quform_element_valid_1_3', function ($valid, $value, Quform_Element_Field $element) { if (username_exists($value)) { $element->addError('This username is already taken'); $valid = false; } return $valid; }, 10, 3); add_filter('quform_element_valid_1_4', function ($valid, $value, Quform_Element_Field $element) { if (email_exists($value)) { $element->addError('This email address is already registered'); $valid = false; } return $valid; }, 10, 3); add_action('quform_post_process_1', function (array $result, Quform_Form $form) { $username = $form->getValueText('quform_1_3'); $email = $form->getValueText('quform_1_4'); $password = $form->getValueText('quform_1_5'); wp_insert_user(array( 'user_login' => $username, 'user_pass' => $password, 'user_email' => $email )); return $result; }, 10, 2); |
add_filter('quform_element_valid_1_3', function ($valid, $value, Quform_Element_Field $element) { if (username_exists($value)) { $element->addError('This username is already taken'); $valid = false; } return $valid; }, 10, 3); add_filter('quform_element_valid_1_4', function ($valid, $value, Quform_Element_Field $element) { if (email_exists($value)) { $element->addError('This email address is already registered'); $valid = false; } return $valid; }, 10, 3); add_action('quform_post_process_1', function (array $result, Quform_Form $form) { $username = $form->getValueText('quform_1_3'); $email = $form->getValueText('quform_1_4'); $password = $form->getValueText('quform_1_5'); wp_insert_user(array( 'user_login' => $username, 'user_pass' => $password, 'user_email' => $email )); return $result; }, 10, 2);
1 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20 2122 23 24 25262728 29 30 31 32 33 34 35 36 37 | function my_check_username_exists($valid, $value, Quform_Element_Field $element) { if (username_exists($value)) { $element->addError('This username is already taken'); $valid = false; } return $valid; } add_filter('quform_element_valid_1_3', 'my_check_username_exists', 10, 3); function my_check_email_exists($valid, $value, Quform_Element_Field $element) { if (email_exists($value)) { $element->addError('This email address is already registered'); $valid = false; } return $valid; } add_filter('quform_element_valid_1_4', 'my_check_email_exists', 10, 3); function my_register_wp_user(array $result, Quform_Form $form) { $username = $form->getValueText('quform_1_3'); $email = $form->getValueText('quform_1_4'); $password = $form->getValueText('quform_1_5'); wp_insert_user(array( 'user_login' => $username, 'user_pass' => $password, 'user_email' => $email )); return $result; } add_action('quform_post_process_1', 'my_register_wp_user', 10, 2); |
function my_check_username_exists($valid, $value, Quform_Element_Field $element) { if (username_exists($value)) { $element->addError('This username is already taken'); $valid = false; } return $valid; } add_filter('quform_element_valid_1_3', 'my_check_username_exists', 10, 3); function my_check_email_exists($valid, $value, Quform_Element_Field $element) { if (email_exists($value)) { $element->addError('This email address is already registered'); $valid = false; } return $valid; } add_filter('quform_element_valid_1_4', 'my_check_email_exists', 10, 3); function my_register_wp_user(array $result, Quform_Form $form) { $username = $form->getValueText('quform_1_3'); $email = $form->getValueText('quform_1_4'); $password = $form->getValueText('quform_1_5'); wp_insert_user(array( 'user_login' => $username, 'user_pass' => $password, 'user_email' => $email )); return $result; } add_action('quform_post_process_1', 'my_register_wp_user', 10, 2);
Adding user meta data (profile fields)
Modify the code to store the created user ID when the user is created by changing this line:
1 | wp_insert_user(array( |
wp_insert_user(array(
To this:
1 | $userId = wp_insert_user(array( |
$userId = wp_insert_user(array(
Then, above this line:
1 | return $result; |
return $result;
You can add calls to the update_user_meta
function, using the $userId
to add user meta data. For example:
1 2 3 | update_user_meta($userId, 'nickname', $form->getValueText('quform_1_6')); update_user_meta($userId, 'first_name', $form->getValueText('quform_1_7')); update_user_meta($userId, 'last_name', $form->getValueText('quform_1_8')); |
update_user_meta($userId, 'nickname', $form->getValueText('quform_1_6')); update_user_meta($userId, 'first_name', $form->getValueText('quform_1_7')); update_user_meta($userId, 'last_name', $form->getValueText('quform_1_8'));
- Replace
nickname
,first_name
andlast_name
with the user meta keys you want to set - Replace
1_6
,1_7
and1_8
with the unique ID of the elements you want to save the value of - Add or remove lines as needed, you may need a developer for further customization
See also
wp_insert_user and update_user_meta at the WordPress Codex