Creating a user

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.

Step 1

Get the form ID, see Finding the form ID.

Step 2

Get the unique element ID for the each of the elements (the username, password and email elements) of the form.

Step 3

Add the following code to the wp-content/themes/YOUR_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.

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
function my_check_username_exists($valid, $value, $element)
{
    if (username_exists($value)) {
        $element->addError('This username is already taken');
        $valid = false;
    }
 
    return $valid;
}
add_filter('iphorm_element_valid_iphorm_2_1', 'my_check_username_exists', 10, 3); 
function my_check_email_exists($valid, $value, $element)
{
    if (email_exists($value)) {
        $element->addError('This email address is already registered');
        $valid = false;
    }
 
    return $valid;
}
add_filter('iphorm_element_valid_iphorm_2_2', 'my_check_email_exists', 10, 3); 
function my_register_wp_user($form)
{
    $username = $form->getValue('iphorm_2_1'); // Change to your username field unique ID    $email = $form->getValue('iphorm_2_2'); // Change to your email field unique ID    $password = $form->getValue('iphorm_2_3'); // Change to your password field unique ID 
    wp_insert_user(array(
        'user_login' => $username,
        'user_pass' => $password,
        'user_email' => $email
    ));
}
add_action('iphorm_post_process_2', 'my_register_wp_user', 10, 1);
function my_check_username_exists($valid, $value, $element)
{
    if (username_exists($value)) {
        $element->addError('This username is already taken');
        $valid = false;
    }

    return $valid;
}
add_filter('iphorm_element_valid_iphorm_2_1', 'my_check_username_exists', 10, 3);

function my_check_email_exists($valid, $value, $element)
{
    if (email_exists($value)) {
        $element->addError('This email address is already registered');
        $valid = false;
    }

    return $valid;
}
add_filter('iphorm_element_valid_iphorm_2_2', 'my_check_email_exists', 10, 3);

function my_register_wp_user($form)
{
    $username = $form->getValue('iphorm_2_1'); // Change to your username field unique ID
    $email = $form->getValue('iphorm_2_2'); // Change to your email field unique ID
    $password = $form->getValue('iphorm_2_3'); // Change to your password field unique ID

    wp_insert_user(array(
        'user_login' => $username,
        'user_pass' => $password,
        'user_email' => $email
    ));
}
add_action('iphorm_post_process_2', 'my_register_wp_user', 10, 1);

Modify the highlighted lines to suit the form setup:

  • On lines 10 and 25, change iphorm_2_1 to the unique ID of the username element, from Step 2
  • On lines 21 and 26, change iphorm_2_2 to the unique ID of the email element, from Step 2
  • On line 27, change iphorm_2_3 to the unique element ID of the password element, from Step 2
  • On line 35, change the number 2 to the form ID, from Step 1

See also

wp_insert_user at the WordPress Codex

Still having trouble? Head over to the forums.

Forums