Sunday, May 31, 2015

How to create custom registration page on frontend side in wordpress?

1) On index.php below get_header();

<?php
            if(!is_user_logged_in())
{
    ?>
    <script>window.location.href = '<?php echo home_url()."/login";?>';</script>
    <?php
    //wp_redirect(home_url()."/login");
}   
?>

2)Create file name "custom-register.php" and paste the following
<?php
/**
 * Template Name: Register Page
 *
 */
get_header();
?>
<?php
function registrationForm() {

            require_once(ABSPATH . WPINC . '/registration.php');
            global $wpdb, $user_ID;
            //Check whether the user is already logged in
            if ($user_ID)
            {
                header( 'Location:' . home_url());
            }
            else
            {
                $errors = array();

                if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['signup'])) {
           
                    // Check username is present and not already in use
                    $username = $wpdb->escape($_REQUEST['username']);
                    if ( strpos($username, ' ') !== false ) {
                        $errors[] = "Sorry, no spaces allowed in usernames";
                    }
                    if(empty($username)) {
                        $errors[] = "Please enter a username";
                    } elseif( username_exists( $username ) ) {
                        $errors[] = "Username already exists, please try another";
                    }
           
                    // Check email address is present and valid
                    $email = $wpdb->escape($_REQUEST['email']);
                    if( !is_email( $email ) ) {
                        $errors[] = "Please enter a valid email";
                    } elseif( email_exists( $email ) ) {
                        $errors[] = "This email address is already in use";
                    }
           
                    // Check password is valid
                    if(0 === preg_match("/.{6,}/", $_POST['password'])){
                      $errors[] = "Password must be at least six characters";
                    }
                   
                    if(0 === count($errors)) {
           
                        $password = $_POST['password'];
                        
                         $new_user_id = wp_create_user( $username, $password, $email );
                       
                        global $wpdb;
                           $url = home_url();
                            $to = $email;
                            $subject = "New Account Verification";

                            $header .= "Reply-To: <test@test.org>\r\n";
                                                        $header .= "Return-Path: <testteam@test.org>\r\n";
                            $headers = "From: ePray Team <test@test.org> \r\n";
                                                        $header .= "Organization: Test.org\r\n";
                            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                                                   
                            $message = "
                                    <b>Dear ".ucwords($username).",<br /><br />Thank you for registering on Test!</b><br /><b>Email : $email</b><br /><br />
                                    But before we can activate your account, one last step must be taken to complete your registration.
                                    Please note, you must complete this last step to become a registered member. You will only need to visit this URL once to activate your account.
                                    To complete your registration, please visit this URL:<br />
                                     After verification : <a href='$url'>Sign in</a><br />
                                    To sign up for our newsletter click <a href='$url/newsletter'>here</a><br /><br />
                                    <b>Best Regards,<br />
                                     Test Team</b>";

                        $res = wp_mail($to, $subject, $message, $headers);
                        $success = 1;
                        $user_id = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."users WHERE user_login = '$username' ",ARRAY_A);
                       
                        $secure_cookie = is_ssl() ? true : false;
                        wp_set_auth_cookie( $user_id['ID'], true, $secure_cookie );
                        //wp_safe_redirect(home_url());
                        ?>
                        <script>window.location.href='<?php echo home_url();?>';</script>
                        <script>
                            message('<?php echo json_encode($errors);?>');
                        </script>
                        <?php
           
                    }
                    else {
                        ?>
                            <script>
                                message('<?php echo json_encode($errors);?>');
                            </script>
                        <?php
                    }
           
                }

            }
        ?>
       
        <div class="signup-form">
                    <form method="post" action="#" id="RegisterForm">
                        <ul>
                            <li>
                                <input type="text" value="" class="text-bx" placeholder="Username" name="username" maxlength="20" autocomplete="off" />
                            </li>
                            <li>
                                <input type="email" value="" class="text-bx" placeholder="Email" name="email" maxlength="60" autocomplete="off" />
                            </li>
                            <li>
                                <input type="password" value="" class="text-bx" placeholder="Password" name="password" maxlength="30" autocomplete="off" />
                            </li>
                            <li>
                                <input type="checkbox" name="terms" id="terms" class="check" /><p>By creating your account you confirm that youve read
                                and accepted the <a href="#terms-content" class="fancybox accpt_te">Terms of Service.</a></p>
                                <input type="submit" name="signup" value="Sign-up for ePray" class="sgn-up" />
                            </li>
                        </ul>
                    </form>
                </div>
           
       
<?php
}
//add_shortcode('mk_registration', 'registrationForm');
registrationForm();
get_footer();
?>

3) Create backend page name "Register", Select template "Custom Register" and publish page.

No comments:

Post a Comment

Please mention your comments.......