On past few days, I was posted an article about creating WordPress Custom Login Page without Using a Plugin and on previous month I’ve posted another wordpress related article which is How to Create Custom Reset or Forget Password in WordPress and to complete wordpress Meta I decided to create a new article “WordPress Custom Registration without Using a Plugin”, this sounds really interesting right? To add registration in your site you can find various wordpress plugins directly in wordpress plugin directory but this tutorial might helpful to all web developer who are willing to learn in wordpress.
In this tutorial we’re using wp_insert_user() function to insert a user into the database, this functions can update a current user or insert a new user based on whether the user’s ID is present.

Syntax

PHP
1
<?php wp_insert_user( $userdata ) ?>

PHP code

This contains php script to validate and accept posted data from our HTML form.
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
$err = ”;
$success = ”;
global $wpdb, $PasswordHash, $current_user, $user_ID;
if(isset($_POST['task']) && $_POST['task'] == ‘register’ ) {
    $pwd1 = $wpdb->escape(trim($_POST['pwd1']));
    $pwd2 = $wpdb->escape(trim($_POST['pwd2']));
    $first_name = $wpdb->escape(trim($_POST['first_name']));
    $last_name = $wpdb->escape(trim($_POST['last_name']));
    $email = $wpdb->escape(trim($_POST['email']));
    $username = $wpdb->escape(trim($_POST['username']));
    if( $email == “” || $pwd1 == “” || $pwd2 == “” || $username == “” || $first_name == “” || $last_name == “”) {
        $err = ‘Please don\’t leave the required fields.’;
    } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $err = ‘Invalid email address.’;
    } else if(email_exists($email) ) {
        $err = ‘Email already exist.’;
    } else if($pwd1 <> $pwd2 ){
        $err = ‘Password do not match.’;
    } else {
        $user_id = wp_insert_user( array (‘first_name’ => apply_filters(‘pre_user_first_name’, $first_name), ‘last_name’ => apply_filters(‘pre_user_last_name’, $last_name), ‘user_pass’ => apply_filters(‘pre_user_user_pass’, $pwd1), ‘user_login’ => apply_filters(‘pre_user_user_login’, $username), ‘user_email’ => apply_filters(‘pre_user_user_email’, $email), ‘role’ => ‘subscriber’ ) );
        if( is_wp_error($user_id) ) {
            $err = ‘Error on user creation.’;
        } else {
            do_action(‘user_register’, $user_id);
            $success = ‘You\’re successfully register’;
        }
    }
}
?>
Lets put in details the function we’re using in our code and its use.
  • email_exists: To look if the user email already exist.
  • apply_filters: Calls filters for most of the $userdata fields with the prefix ‘pre_user’, where $userdata is the data contain to create user.
  • do_action: Calls ‘user_register’ hook when creating a new user giving the user’s ID.

HTML code

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<form method=”post”>
<h3>Don’t have an account?<br/> Create one now.</h3>
<p><label>Last Name</label></p>
<p><input type=”text” value=”" name=”last_name” id=”last_name” /></p>
<p><label>First Name</label></p>
<p><input type=”text” value=”" name=”first_name” id=”first_name” /></p>
<p><label>Email</label></p>
<p><input type=”text” value=”" name=”email” id=”email” /></p>
<p><label>Username</label></p>
<p><input type=”text” value=”" name=”username” id=”username” /></p>
<p><label>Password</label></p>
<p><input type=”password” value=”" name=”pwd1″ id=”pwd1″ /></p>
<p><label>Password again</label></p>
<p><input type=”password” value=”" name=”pwd2″ id=”pwd2″ /></p>
<div class=”alignleft”><p><?php if($sucess != “”) { echo $sucess; } ?> <?php if($err != “”) { echo $err; } ?></p></div>
<button type=”submit” name=”btnregister” class=”button” >Submit</button>
<input type=”hidden” name=”task” value=”register” />
</form>
I’ve added one hidden field to validate if the submitted form is for registration.

Parameters

$userdata An array of user data.

Complete Source Code

Below is the complete custom registration page source code, copy and paste the snippet below to test it yourself.
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
Template Name: Login Page
*/
the_post()
get_header()
?>
<div class=”wrapper”>
    <?php
    $err = ”;
    $success = ”;
    global $wpdb, $PasswordHash, $current_user, $user_ID;
    if(isset($_POST['task']) && $_POST['task'] == ‘register’ ) {
        $pwd1 = $wpdb->escape(trim($_POST['pwd1']));
        $pwd2 = $wpdb->escape(trim($_POST['pwd2']));
        $first_name = $wpdb->escape(trim($_POST['first_name']));
        $last_name = $wpdb->escape(trim($_POST['last_name']));
        $email = $wpdb->escape(trim($_POST['email']));
        $username = $wpdb->escape(trim($_POST['username']));
        if( $email == “” || $pwd1 == “” || $pwd2 == “” || $username == “” || $first_name == “” || $last_name == “”) {
            $err = ‘Please don\’t leave the required fields.’;
        } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $err = ‘Invalid email address.’;
        } else if(email_exists($email) ) {
            $err = ‘Email already exist.’;
        } else if($pwd1 <> $pwd2 ){
            $err = ‘Password do not match.’;
        } else {
            $user_id = wp_insert_user( array (‘first_name’ => apply_filters(‘pre_user_first_name’, $first_name), ‘last_name’ => apply_filters(‘pre_user_last_name’, $last_name), ‘user_pass’ => apply_filters(‘pre_user_user_pass’, $pwd1), ‘user_login’ => apply_filters(‘pre_user_user_login’, $username), ‘user_email’ => apply_filters(‘pre_user_user_email’, $email), ‘role’ => ‘subscriber’ ) );
            if( is_wp_error($user_id) ) {
                $err = ‘Error on user creation.’;
            } else {
                do_action(‘user_register’, $user_id);
                $success = ‘You\’re successfully register’;
            }
        }
    }
    ?>
        <!–display error/success message–>
    <div id=”message”>
        <?php
            if(! empty($err) ) :
                echo ‘<p>’.$err.’</p>’;
            endif;
        ?>
        <?php
            if(! empty($success) ) :
                echo ‘<p>’.$success.’</p>’;
            endif;
        ?>
    </div>
    <form method=”post”>
        <h3>Don’t have an account?<br/> Create one now.</h3>
        <p><label>Last Name</label></p>
        <p><input type=”text” value=”" name=”last_name” id=”last_name” /></p>
        <p><label>First Name</label></p>
        <p><input type=”text” value=”" name=”first_name” id=”first_name” /></p>
        <p><label>Email</label></p>
        <p><input type=”text” value=”" name=”email” id=”email” /></p>
        <p><label>Username</label></p>
        <p><input type=”text” value=”" name=”username” id=”username” /></p>
        <p><label>Password</label></p>
        <p><input type=”password” value=”" name=”pwd1″ id=”pwd1″ /></p>
        <p><label>Password again</label></p>
        <p><input type=”password” value=”" name=”pwd2″ id=”pwd2″ /></p>
        <div class=”alignleft”><p><?php if($sucess != “”) { echo $sucess; } ?> <?php if($err != “”) { echo $err; } ?></p></div>
        <button type=”submit” name=”btnregister” class=”button” >Submit</button>
        <input type=”hidden” name=”task” value=”register” />
    </form>
</div>
<?php get_footer() ?>
The detailed basic tutorial on how to create custom pate template can be found at WP Codex.
That’s it happy coding, if you have question, clarification just use the comment below or contact me directly.

0 comments:

Post a Comment

 
Top