Note: This code is old, and it may not work with the latest versions of WordPress
WordPress provides a function to echo in front end a simple login form ( wp_login_form( $args ) ), but it doesn’t provide a simple way to embed the registration form ( the form displayed in /wp-login.php?action=register ) in a regular page or sidebar.
After some google-ing and trying a few plugins, i decided it is more flexible if i make one myself. In my research to do this I stumble upon this article here which is the base of what i will explain below.
Make your own WordPress registration form:
Step 1
Create a file called ‘registration_form.php’ in your theme in which paste the code below.
<div class="registration-form-wrapper"> <?php /* the captcha result is stored in session */ session_start(); if( isset( $_POST['svalue'] ) ) { if($_POST['svalue'] != $_SESSION['answer']) { $matherror = "Wrong math!"; } else { $matherror = " "; } } include('arithmetic_captcha.php'); if(defined('REGISTRATION_ERROR')){ foreach(unserialize(REGISTRATION_ERROR) as $error){ echo '<p class="error">'.$error.'</p'; } } elseif(defined('REGISTERED_A_USER')){ echo '<p class="success">Successful registration, an email has been sent to '.REGISTERED_A_USER .'</p>'; } echo '<p class="error">'. $matherror .'</p>'; ?> <form id="my-registration-form" method="post" action="<?php echo add_query_arg('do', 'register', get_permalink( $post->ID )); ?>"> <ul> <li> <label for="username">Username</label> <input type="text" id="username" name="user" value=""/> </li> <li> <label for="email">E-mail</label> <input type="text" id="email" name="email" value="" /> </li> <li> What is the result <strong><?php echo $math;?></strong> ? <input type="text" name="svalue" value="" size="7" /> </li> <li> <input type="submit" value="Register" /> </li> </ul> </form> </div>
This is basically the HTML of the form.
Step 2
Add the ‘register user’ function, which you will find below, to your theme’s ‘functions.php‘ file, ..this will register the user and validate the form.
add_action('template_redirect', 'register_a_user'); function register_a_user(){ if(isset($_GET['do']) && $_GET['do'] == 'register'): $errors = array(); if(empty($_POST['user']) || empty($_POST['email'])) $errors[] = 'Please enter a user name and e-mail.'; $user_login = esc_attr($_POST['user']); $user_email = esc_attr($_POST['email']); $sanitized_user_login = sanitize_user($user_login); $user_email = apply_filters('user_registration_email', $user_email); if(!is_email($user_email)) $errors[] = 'Invalid e-mail.'; elseif(email_exists($user_email)) $errors[] = 'This email is already registered.'; if(empty($sanitized_user_login) || !validate_username($user_login)) $errors[] = 'Invalid user name.'; elseif(username_exists($sanitized_user_login)) $errors[] = 'User name already exists.'; if(empty($errors)): $user_pass = wp_generate_password(); $user_id = wp_create_user($sanitized_user_login, $user_pass, $user_email); if(!$user_id): $errors[] = 'Registration failed'; else: update_user_option($user_id, 'default_password_nag', true, true); wp_new_user_notification($user_id, $user_pass); endif; endif; if(!empty($errors)) define('REGISTRATION_ERROR', serialize($errors)); else define('REGISTERED_A_USER', $user_email); endif; }
Step 3
Create a file called ‘arithmetic_captcha.php‘ which will contain a simple random numbers generator, for a simple ‘math captcha’ to keep the spam away. This simple captcha can be used with other forms as well, …just use the code below and the check made at Step 1.
<?php session_start(); $nr1 = mt_rand(1,10); //mt_rand($min, $max) $nr2 = mt_rand(1,10); if( $nr1 > $nr2 ) { $math = "$nr1 - $nr2"; $_SESSION['answer'] = $nr1 - $nr2; } else { $math = "$nr1 + $nr2"; $_SESSION['answer'] = $nr1 + $nr2; }
Step 4
Just include the ‘registration_form.php’ file created at Step 1 in your page template or wherever you want the form to appear.
<?php include('registration_form.php');?>
Or you can just download the files here (wp_registration_form.zip) and include them in your theme.
That works! Thank you very much!
Works great, thank you!
Only one minor issue, in Step 4 you should add . Quick fix.
Otherwise good job.
rename include(‘register_form.php’); to include(‘registration_form.php’);.
Thank you for pointing out that mistake, and thank you for your comment.
Good afternoon,
I’m currently working with the snippets above but when I try adding more fields such as User URL & User Phone Number nothing seems to be working, I’ve played with the front end form and the functions.php scripts.
Any advice on adding more feels would be much appreciated.
Thank you
Adam @ AWKREATIV
Hi Adam,
These snippets are not a custom registration form, they just replicate the default registration form.
Try using a plugin, like http://wordpress.org/extend/plugins/cimy-user-extra-fields/
Hope it helps.
nice trick wow
Is there any fix for this problem:
The submit register works only once in the registration page but doesn’t work when it gets to the register/?do=register
?? Any help would be greatly appreciated.
Hi,
I do not completely understand the problem, but I can say this:
The ‘/?do=register’ is the registration form action, so at this step you shouldn’t have a submit button.
Hi!
Thank you for this great work.
I was looking for that since 2 days.
How can I do if I want to display the first_name and last_name in the form?
Thank you for your help.
Hi,
Refer to this article https://codex.wordpress.org/Customizing_the_Registration_Form for more info.
Hello,
I am getting this error on my fronted regular page.
Please help me if possible.
Thank you.
Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:\websites\blog\wp-content\themes\hemo-theme\header.php:86) in C:\websites\blog\wp-content\themes\hemo-theme\registration_form.php on line 4
I haven’t used this code for some time now, and I don’t really know what it’s compatibility with the latest versions of wordpress is.
I can’t figure out why you are getting those errors, but you should try removing the captcha from registration_form.php (everything from line 3 to 15).
Thanks