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);
  • On lines 1 and 20, change 1_3 to the unique ID of the username Text element
  • On lines 10 and 21, change 1_4 to the unique ID of the Email element
  • On line 19, change the number 1 to the form ID
  • On line 22, change 1_5 to the unique ID of the Password element

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 and last_name with the user meta keys you want to set
  • Replace 1_6, 1_7 and 1_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

Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy