Websites commonly have extra register and login feature. With WordPress, the default feature has contained too many disadvantages that need to be edited. There are many plugins supporting this feature, however, the plugin installation may cause errors and make the website more slowly. It is the reason why this article how to create registration form in wordpress without plugin is launched.

how to create registration form in wordpress without plugin

Using plugin in WordPress is quite convenient because it helps save time without any knowledge about code. But it is a disadvantage when you cannot customize the site as your needs. If you use plugins that are not downloaded from the home page or plugins that are edited by bad people, they may be so harmful to your website. In order to get over the problem, using code is the most optimum.

“ How to create registration form in wordpress without plugin ” is the article which helps create a registration form. You do not need to know much about code because this article will give you a detailed guidance. Also, you can download the code file at the end of the article.

Create register page

Firstly, you create a page-registration-custom.php file in the current theme folder. This is the first code that is to declare a new template. We will choose one page to run the code in this file. You should save the file before move on to the next steps.

<?php

/*

** Template Name: Register Page Custom

*/

?>

get_header();

?>

Next, get to back office, create one template include a template name as below

how to create registration form in wordpress without plugin

Check if a member has logged in or not We will check this in this page. If he has logged in, we will redirect to the home page. If not, there will be a form for him to register.  The code is as below

<?php

global $wpdb, $user_ID; 

if (!$user_ID) { 

   //All code goes in here. 
} 
else { 
wp_redirect(home_url() ); exit; 
}
?>

Create a register form

Look at the form below, you can fields to your project properly

<h3>Create your account</h3>

<form action="" method="post" name="user_registeration">

<label>Username <span class="error">*</span></label>

<input type="text" name="username" placeholder="Enter Your Username" class="text" required /><br />

<label>Email address <span class="error">*</span></label>

<input type="text" name="useremail" class="text" placeholder="Enter Your Email" required /><br />

<label>Password <span class="error">*</span></label>

<input type="password" name="password" class="text" placeholder="Enter Your password" required /><br />

<input type="submit" name="user_registeration" value="SignUp" />

</form>

<?php if(isset($signUpError)){echo '<div>'.$signUpError.'</div>';}?>

 

Check the information and account

After users click on the register button, data will be processed. We will check the input data and announce if the registration process is successful or not. Or we will announce the error if any. For example, a valid email must include @ and domain. If the input email is not contained in the data, it will be used to register an account.

<?php

if (isset($_POST['user_registeration']))
{
    //registration_validation($_POST['username'], $_POST['useremail']);
global $reg_errors;
    $reg_errors = new WP_Error;
    $username=$_POST['username'];
    $useremail=$_POST['useremail'];
    $password=$_POST['password'];
if(empty( $username ) || empty( $useremail ) || empty($password))
    {
       $reg_errors->add('field', 'Required form field is missing');
    }   
if ( 6 >strlen( $username ) )
    {
       $reg_errors->add('username_length', 'Username too short. At least 6 characters is required' );
    }
if ( username_exists( $username ) )
   {
       $reg_errors->add('user_name', 'The username you entered already exists!');
   }
if ( ! validate_username( $username ) )
    {
        $reg_errors->add( 'username_invalid', 'The username you entered is not valid!' );
    }
if ( !is_email( $useremail ) )
    {
        $reg_errors->add( 'email_invalid', 'Email id is not valid!' );

   }

if ( email_exists( $useremail ) )
   {
       $reg_errors->add( 'email', 'Email Already exist!' );
    }
if ( 5 >strlen( $password ) ) {
        $reg_errors->add( 'password', 'Password length must be greater than 5!' );
    }
if (is_wp_error( $reg_errors ))
    {
foreach ( $reg_errors->get_error_messages() as $error )
        {
            $signUpError='<p style="color:#FF0000; text-aling:left;"><strong>ERROR</strong>: '.$error . '<br /></p>';
        }
    }
if ( 1 > count( $reg_errors->get_error_messages() ) )
    {
        // sanitize user form input
global $username, $useremail;
       $username   =   sanitize_user( $_POST['username'] );
       $useremail  =sanitize_email( $_POST['useremail'] );
        $password   =   esc_attr( $_POST['password'] );
        $userdata = array(
           'user_login'    =>   $username,
            'user_email'    =>   $useremail,
           'user_pass'     =>   $password,
            );

        $user = wp_insert_user( $userdata );
    }

}

?>

We have finished the article how to create registration form in wordpress without plugin. It is easy to add fields if you read this article. If you feel this article is helpful, please share it.You could view more articles about custom field in wordpress. If you any question or recommendation, please leave a comment or contact us.

Leave a Reply

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