WordPress registration form in a regular page

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> ? &nbsp; <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.

Comments

  1. written by: Goran on December 16, 2012 at 7:33 pm - Reply

    That works! Thank you very much!

  2. written by: Stefan on February 15, 2013 at 2:08 pm - Reply

    Works great, thank you!
    Only one minor issue, in Step 4 you should add . Quick fix.
    Otherwise good job.

    • written by: Stefan on February 18, 2013 at 4:39 pm - Reply

      rename include(‘register_form.php’); to include(‘registration_form.php’);.

      • written by: mihai on March 2, 2013 at 2:18 pm - Reply

        Thank you for pointing out that mistake, and thank you for your comment.

  3. written by: Adam Wadsworth [at] awkreativ on April 25, 2013 at 6:47 pm - Reply

    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

  4. written by: waheed on August 25, 2013 at 10:01 pm - Reply

    nice trick wow

  5. written by: halben on September 12, 2013 at 4:18 pm - Reply

    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.

    • written by: mihai on September 12, 2013 at 10:25 pm - Reply

      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.

  6. written by: sika on September 27, 2013 at 7:32 pm - Reply

    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.

  7. written by: Keshav Naidu on July 10, 2014 at 11:36 pm - Reply

    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

    • written by: Mihai on July 11, 2014 at 2:38 pm - Reply

      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).

  8. written by: Guru on July 23, 2014 at 7:38 am - Reply

    Thanks

Leave a Reply to Keshav Naidu Cancel reply

Your email address will not be published. Required fields are marked *

1 × = 1

This site uses Akismet to reduce spam. Learn how your comment data is processed.