Creating a user

The Quform WordPress v2 documentation is not yet finished. Please post on the forums if you have any questions in the mean time.

Click here to visit the complete documentation for Quform 1.x

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->setError('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->setError('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->getValue('quform_1_3');    $email = $form->getValue('quform_1_4');    $password = $form->getValue('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->setError('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->setError('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->getValue('quform_1_3');
    $email = $form->getValue('quform_1_4');
    $password = $form->getValue('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
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->setError('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->setError('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->getValue('quform_1_3');    $email = $form->getValue('quform_1_4');    $password = $form->getValue('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->setError('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->setError('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->getValue('quform_1_3');
    $email = $form->getValue('quform_1_4');
    $password = $form->getValue('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);
  • On lines 10 and 25, change 1_3 to the unique ID of the username Text element
  • On lines 21 and 26, change 1_4 to the unique ID of the Email element
  • On line 27, change 1_5 to the unique ID of the Password element
  • On line 37, change the number 1 to the form ID

See also

wp_insert_user at the WordPress Codex

Still having trouble? Head over to the forums.

Forums