How Create Registration page template
Registration page template
A complete tutorial about how to create a page template where users can register. The whole code could certainly have been better, but still a big time saver if you’re in a hurry.
Step1: Create page custom-register.php
First we will create a new php template file called custom-register.php and place it inside your wordpress theme folder your-domain-name/wp-content/themes/your-theme-name
Step2: Naming the Template file
view plaincopy to clipboardprint?
< ?php /* Template Name: Custom WordPress Registration */ ?>
Step3: check if the user is not logged in
Like we did for custom login, we must first check whether the current user is logged in or not. We will show the registration form only if the current user is not logged in. Additionally, we need to include the file registration.php from wp-includes folder in order to create a new user inside wordpress system.
view plaincopy to clipboardprint?
require_once(ABSPATH . WPINC . ‘/registration.php’);
global $wpdb, $user_ID;
if (!$user_ID) {
//All code goes in here.
}
else {
wp_redirect( home_url() ); exit;
}
Step4: Embedding the Register Form and jQuery Ajax
Before we display the register form we need to check whether the user registration is allowed by the administrator using the function get_option(‘users_can_register’).
view plaincopy to clipboardprint?
< ?php if(get_option(‘users_can_register’)) { //Check whether user registration is enabled by the administrator ?>
< ?php the_title(); ?>
< ?php } else echo “Registration is currently disabled. Please try again later.”; ?>
Step5: Validate the inputs and register the user
Add the following php code inside if (!$user_ID) { } and move the register form inside the else part of the following if condition.
view plaincopy to clipboardprint?
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_REQUEST[‘username’]);
if(emptyempty($username)) {
echo “User name should not be empty.”;
exit();
}
$email = $wpdb->escape($_REQUEST[‘email’]);
if(!preg_match(“/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$/”, $email)) {
echo “Please enter a valid email.”;
exit();
}
$random_password = wp_generate_password( 12, false );
$status = wp_create_user( $username, $random_password, $email );
if ( is_wp_error($status) )
echo “Username already exists. Please try another one.”;
else {
$from = get_option(‘admin_email’);
$headers = ‘From: ‘.$from . “rn”;
$subject = “Registration successful”;
$msg = “Registration successful.nYour login detailsnUsername: $usernamenPassword: $random_password”;
wp_mail( $email, $subject, $msg, $headers );
echo “Please check your email for login details.”;
}
exit();
}
else
{
//Embed the register form and javascript here
}
In the above code, we will validate the inputs and then create random password with the use of function wp_generate_password(). With the use of wp_create_user function create a new user account if not the username already exists in the system. Finally, we will email the login details including the random generated password to the signed up user.
Full Code Preview
view plaincopy to clipboardprint?
< ?php /* Template Name: Custom WordPress Signup Page */ require_once(ABSPATH . WPINC . ‘/registration.php’); global $wpdb, $user_ID; //Check whether the user is already logged in if (!$user_ID) { if($_POST){ //We shall SQL escape all inputs $username = $wpdb->escape($_REQUEST[‘username’]);
if(emptyempty($username)) {
echo “User name should not be empty.”;
exit();
}
$email = $wpdb->escape($_REQUEST[‘email’]);
if(!preg_match(“/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$/”, $email)) {
echo “Please enter a valid email.”;
exit();
}
$random_password = wp_generate_password( 12, false );
$status = wp_create_user( $username, $random_password, $email );
if ( is_wp_error($status) )
echo “Username already exists. Please try another one.”;
else {
$from = get_option(‘admin_email’);
$headers = ‘From: ‘.$from . “rn”;
$subject = “Registration successful”;
$msg = “Registration successful.nYour login detailsnUsername: $usernamenPassword: $random_password”;
wp_mail( $email, $subject, $msg, $headers );
echo “Please check your email for login details.”;
}
exit();
} else {
get_header();
?>
< ?php if(get_option('users_can_register')) { //Check whether user registration is enabled by the administrator ?>
< ?php the_title(); ?>
< ?php } else echo "Registration is currently disabled. Please try again later."; ?>
< ?php get_footer(); } //end of if($_post) } else { wp_redirect( home_url() ); exit; } ?>
Once the template file is ready
Link: http://www.tutorialstag.com/create-custom-wordpress-registration-page.html