
How To Create A New User Form From Front End
- August 20, 2015
- Leave a comment
WordPress provides a custom user form which can be used to register a new user. It only requires a user name and email. After successful signup, password will be emailed back to you. Next time when you login to the website with the password that was sent you back in email and you complete your profile, after that you need to change your password again to something that will be easy for you to remember.
But what if you want to make your own custom registration form? Instead of user to go through the above mentioned stress, why not register by a form where you can place consistent information like user name, password, first name, last name, biography etc.
These are the default custom fields in WordPress User Profile. You can add your custom fields to it as well. For this, firstly you need to create a user form using the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<form method="post" class="wp-user-form" id="wp_signup_form" action="" enctype="multipart/form-data"> <ul class="upload-file"> <li> <input type="text" name="user_name" placeholder="User Name" value="" id="user_name" /> </li> <li> <input type="text" autocomplete="off" name="user_first_name" value="" placeholder="First Name" id="user_first_name" /> </li> <li> <input type="text" autocomplete="off" name="user_last_name" value="" placeholder="Last Name" id="user_flast_name" /> </li> <li> <input type="email" autocomplete="off" name="user_email" value="" placeholder="Email" id="user_email" /> </li> <li> <input type="password" autocomplete="off" name="user_password" id="user_password" value="" placeholder="Password" id="user_password" /> </li> <li> <input type="password" autocomplete="off" name="repeat_user_password" value="" placeholder="Repeat Password" size="25" id="user_password" /> </li> <li> <input type="phone" name="user_phone" value="" placeholder="(123) 456-7890" id="user_phone" /> </li> <li> <textarea name="user_address" id="user_address" placeholder="Street Address"></textarea> </li> </ul> <ul class="upload-file"> <li> <input type="submit" name="user-submit" value="Register Now" class="user-submit backcolr" onclick=""/> </li> </ul> </form> |
After the submission of form, you can check the required fields and ask your user to fill those fields:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
If ( isset( $_POST[‘user-submit’] ) ) { $user_name = $_POST[‘user_name’], $user_email = $_POST[‘user_email’], $first_name = $_POST[‘user_first_name’], $last_name = $_POST[‘user_last_name’], $user_password = $_POST[‘user_password’], $repeat_user_password = $_POST[‘repeat_user_password], $user_address = $_POST[‘user_address’], $user_phone = $_POST[‘user_phone’], $reg_errors = new WP_Error; $error = '0'; if ( empty ( $user_name] ) || empty ( $user_password ) || empty ( $user_email ) || empty ( $user_phone ) || empty ( $user_address ) ) { $reg_errors->add ( 'fields', 'Required form field is missing' ); $error++; } } |
Further, you can validate the email and other field values here as well. You can also check the username and email id if it exists or not.
To check if username already exists or not, use the following code:
1 2 3 4 |
if ( username_exists ( $user_name ) ) { $reg_errors->add ( 'user_name', 'Sorry, that username already exists!' ); $error++; } |
For validating username, use the following code:
1 2 3 4 |
if ( !validate_username ( $user_name ) ) { $reg_errors->add ( 'username_invalid', 'Sorry, the username you entered is not valid'); $error++; } |
To check if email ID is valid or not, use the following code:
1 2 3 4 5 |
If ( !is_email( $user_email )){ $reg_errors->add ( 'validate_email', 'Sorry, the email Id is not valid email' ); $error++; } |
To check if email address already exists or not, use the following code:
1 2 3 4 |
if ( email_exists ( $user_email ) ) { $reg_errors->add ( 'useremail', 'Sorry, the User email already exists' ); $error++; } |
For validating Password, use the following code:
1 2 3 4 5 6 7 8 9 10 |
if ( 5 > strlen ( $user_password ) and $user_password != '' ) { $reg_errors->add ( 'user_password', 'Password length must be greater than 5' ); $error++; } else if ( !empty ( $user_password ) && !empty ( $repeat_user_password ) ) { if ( $user_password != $repeat_user_password ) { $reg_errors->add ( 'user_password', 'Password doesn't match' ); $error++; } } |
If any required field is missed or any field validation is false then print this ‘$error’. Its value should be greater than 0, error messages will be displayed. If $error’s value is equal to 1 then all the fields are good to go. Now we have to create a new user:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
if ( is_wp_error ( $reg_errors ) and $error != '0' ) { $error_message = ‘’; foreach ( $reg_errors->get_error_messages () as $errors ) { $error_message .= $errors.'<br>' ; } } else if ( 1 > count ( $reg_errors->get_error_messages () ) and $error == '0' ) { $userdata = array ( 'user_login' => $user_name, 'user_email' => $user_email, 'first_name' => $user_first_name, 'last_name' => $user_last_name, 'user_pass' => $user_password, 'nickname' => $user_name, 'address' => $user_address, 'phone' => $user_phone, ); $user_id = wp_insert_user ( $userdata ); If( ! is_wp_error( $user_id ) ){ echo ‘User Registered successfully.’; }else{ echo $user_id->get_error_message(); } } |
wp_inser_user() will return the id of current user inserted on success, else it will return the wp_error object.
On success, if you want to login a current registered user, then you can do this by using the following code:
1 2 3 4 |
wp_set_current_user( $user_id, $user_name); wp_set_auth_cookie( $user_id ); wp_set_auth_cookie( $user_id ); do_action( 'wp_login', $user_name); |
You can make this form by using a shortcode or by any custom template. Now you can style it with respect to the design of your theme.
1 |
do_action( 'wp_login', $user_name); |
User Comments