1

How can we create register form without using a plugin in wordpress theme development and how the form datas can be saved in database? since i am new to theme development, i have no idea..help me out!!!i have this simple register form and i dont know how to implement this in wordpress without using a plugin in my theme.i want to know how user registeration mechanism works in wordpress.

        <div class="col-md-6 login-do">
            <input type="hidden" name="action" value="add_foobar">
            <input type="hidden" name="data" value="foobarid">

            <div class="login-mail">
                <input type="text" placeholder="Name"  name='user_name' required="">
                <i  class="glyphicon glyphicon-user"></i>
            </div>
            <div class="login-mail">
                <input type="text" placeholder="Phone Number" name='user_number' required="">
                <i  class="glyphicon glyphicon-phone"></i>
            </div>
            <div class="login-mail">
                <input type="text" placeholder="Email" name='user_email' required="">
                <i  class="glyphicon glyphicon-envelope"></i>
            </div>
            <div class="login-mail">
                <input type="password" placeholder="Password" name='user_password' required="">
                <i class="glyphicon glyphicon-lock"></i>
            </div>
            <a class="news-letter " href="#">
                <label class="checkbox1"><input type="checkbox" name="checkbox" ><i> </i>Forget Password</label>
            </a>
            <label class="hvr-skew-backward">
                <input type="submit" value="Submit" name="submit">
            </label>
        </div>

1 Answer 1

1

In your PHP, use wp_create_user().

With this function, you can pass username, email and `password'.

Then, wp_update_user() to give the user the other bits of info.

I would hook my user-creation function to the init hook.

Also, you want to put a nonce field in your form e.g. this, between the <form> tags:

<?php wp_nonce_field( 'create_user_form_submit', 'djie3duhb3edub3u' ); ?>

E.g.

add_action('init', 'my_theme_create_new_user');
function my_theme_create_new_user(){

    if ( 
        ! isset( $_POST['djie3duhb3edub3u'] ) 
        || ! wp_verify_nonce( $_POST['djie3duhb3edub3u'], 'create_user_form_submit') 
    ){
       // do nothing
    }else{
        $username = sanitize_text_field($_POST['user_name']);
        $email = sanitize_text_field($_POST['user_email']);
        $password = $_POST['user_password'];
        $user_id = username_exists( $username );
        if ( !$user_id and email_exists($email) === false ) {
        
            // do some code to validate password how ever you want it.

            $user_id = wp_create_user( $username, $password, $email );
            $stuff = array('ID'=>$user_id,'another_user_field'=>'something');
            wp_update_user($stuff) // first name, last name etc
         } else {
             return false; //username exists already
         }
     }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.