how can I insert data into wordpress table(default)

Just want to create the custom registration form and the code what I did for it is below. Everything is going as per my plan but when I add the value in registration form than neither I see any error nor see any records which is insert into database. Any body help me to sort this problem.

    <?php

function super_registration(){
    if(isset( $_POST['submit'])){
        /*registration_validation(
        $_POST['uname'],
        $_POST['pwd'],
        $_POST['email'],
        $_POST['weburl'],
        $_POST['fname'],
        $_POST['lname'],
        $_POST['nname'],
        $_POST['bio']
        );

        global $uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio;
        $uname  = sanitize_user($_POST['uname']);
        $pwd    = esc_attr($_POST['pwd']);
        $email  = sanitize_email($_POST['email']);
        $weburl = esc_url($_POST['weburl']);
        $fname  = sanitize_text_field($_POST['fname']);
        $lname  = sanitize_text_field($_POST['lname']);
        $nname  = sanitize_text_field($_POST['nname']);
        $bio    = esc_textarea($_POST['bio']);

        final_registration($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio);    
    }
    Registration_form($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio);*/

    echo "test";
    exit;
    }
}

function registration_form($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio){
    echo '
    <style>
    div {
    margin-bottom: 2px;
    }
    input {
    margin-bottom: 4px;
    }
    </style>
    ';

    echo '
    <form action="' . $_SERVER['Request_URI'] . '"method = "post">

    <div>
    <label for="uname"> User Name <Strong>*</strong></label>
    <input type="text" name="uname" value= "' . (isset( $_POST['uname']) ? $uname : NULL) .'">
    <div>

    <div>
    <label for="pwd"> Password <Strong>*</strong></label>
    <input type="password" name="pwd" values="' . (isset($_POST['pwd']) ? $pwd : NULL) . '">
    </div>

    <div>
    <label for="email"> Email Address <Strong>*</strong></label>
    <input type="text" name="email" values="' . (isset($_POST['email']) ? $email : NULL) . '">
    </div>      

    <div>
    <label for="weburl"> Website URL </label>
    <input type="text" name="weburl" values="' . (isset($_POST['weburl']) ? $weburl : NULL) . '">
    </div>

    <div>
    <label for="fname"> First Name </label>
    <input type="text" name="fname" values="' . (isset($_POST['fname']) ? $fname : NULL) . '">
    </div>  

    <div>
    <label for="lname"> Last Name </label>
    <input type="text" name="lname" values="' . (isset($_POST['lname']) ? $lname : NULL) . '">
    </div>      

    <div>
    <label for="nname"> Nick Name </label>
    <input type="text" name="nname" values="' . (isset($_POST['nname']) ? $nname : NULL) . '">
    </div>      

    <div>
    <label for="bio">About  </label>
    <textarea name="bio">' . (isset($_POST['bio']) ? $bio : NULL) . '</textarea>
    </div>

    <input type="submit" name="Submit" value="register" />
    </form> ';          

}

function registration_validation($uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio){
    global $regerror;
    $regerror = new WP_Error;

    if( empty($uname) || empty($pwd) || empty($email)){
        $regerror->add('field','Required Field are Missing');
    }

/*  if( strlen($uname) < 4){
        $regerror->add('uname_length','Enter Username containing more than 4 character');
    }

    if( uname_exits($uname)){
        $regerror->add('uname','User name already exits please enter different user name');
    }

    if( ! validate_uname( $uname ) ){
        $regerror->add( 'uname_invalid', 'Sorry, the username you entered is not valid' );
    }

    if ( strlen( $pwd ) < 5 ) {
        $regerror->add('pwd', 'Password length must be greater than 5');
    }

    if ( !is_email( $email ) ) {
        $regerror->add('email_invalid', 'Email is not valid');
    }

    if ( email_exists( $email ) ) {
        $regerror->add('email', 'Email Already in use');
    }

    if ( !empty( $weburl ) ) {
        if ( !filter_var($weburl, FILTER_VALIDATE_URL) ) {
            $regerror->add('weburl', 'Website is not a valid URL');
        }
    }
*/
    if ( is_wp_error( $regerror ) ) {

        foreach ( $regerror->get_error_messages() as $error ) {
            echo '<div>';
            echo '<strong>ERROR</strong>:';
            echo $error . '<br/>';
            echo '</div>';
        }
    }
}

function final_registration(){
    global $regerror, $uname, $pwd, $email, $weburl, $fname, $lname, $nname, $bio;
    if(count($regerror->get_error_messages()) < 1){
        $udata = array(
        'user_login'  => $uname,
        'user_pass'   => $pwd,
        'user_email'  => $email,
        'user_url'    => $weburl,
        'first_name'  => $fname,
        'last_name'   => $lname,
        'nickname'    => $nname,
        'description' => $bio,
);

    $user = wp_insert_user($udata);
    echo 'Registration Successfully Done . goto <a href="' .get_site_url() . '/wp-login.php">Login Page</a>.';
    }
}

add_shortcode('srt_super_registration','super_registration_shortcode');
function super_registration_shortcode(){
    ob_start();
    super_registration();
    return ob_get_clean();
}

?>

Related posts

Leave a Reply

1 comment

  1. There is error in your form tag “=” is missing.pl change with following.

    echo '
    <form action="' . $_SERVER['Request_URI'] . '" method = "post">
    

    Also your argument in $udata is wrong

    $udata = array(
        'user_login'     => $uname,
        'user_pass'   => $pwd,
        'user_email' => $email,
        'user_url'    => $weburl,
        'first_name' => $fname,
        'last_name'        => $lname,
        'nickname'     => $nname,
        'description'       => $bio,
    );