Advertisement
Guest User

registration

a guest
Sep 6th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.24 KB | None | 0 0
  1. <?php
  2. ob_start();
  3. function custom_registration_function() {
  4.  
  5.     $username = $password = $email = $website = $first_name = $last_name = $nickname = $bio = '';
  6.  
  7.         if (isset($_POST['submit'])) {
  8.             registration_validation(
  9.                 sanitize_user($_POST['username']),
  10.                 esc_attr($_POST['password']),
  11.                 sanitize_email($_POST['email']),
  12.                 esc_url($_POST['website']),
  13.                 sanitize_text_field($_POST['fname']),
  14.                 sanitize_text_field($_POST['lname']),
  15.                 sanitize_text_field($_POST['nickname']),
  16.                 esc_textarea($_POST['bio'])
  17.             );
  18.            
  19.             // sanitize user form input
  20.             global $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;
  21.             $username   =   sanitize_user($_POST['username']);
  22.             $password   =   esc_attr($_POST['password']);
  23.             $email      =   sanitize_email($_POST['email']);
  24.             $website    =   esc_url($_POST['website']);
  25.             $first_name =   sanitize_text_field($_POST['fname']);
  26.             $last_name  =   sanitize_text_field($_POST['lname']);
  27.             $nickname   =   sanitize_text_field($_POST['nickname']);
  28.             $bio        =   esc_textarea($_POST['bio']);
  29.  
  30.             // call @function complete_registration to create the user
  31.             // only when no WP_error is found
  32.             complete_registration(
  33.             $username,
  34.             $password,
  35.             $email,
  36.             $website,
  37.             $first_name,
  38.             $last_name,
  39.             $nickname,
  40.             $bio
  41.             );
  42.         }
  43.  
  44.         registration_form(
  45.             $username,
  46.             $password,
  47.             $email,
  48.             $website,
  49.             $first_name,
  50.             $last_name,
  51.             $nickname,
  52.             $bio
  53.             );
  54.     }
  55.  
  56.  
  57.  
  58.     function registration_form( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) {
  59.         echo '
  60.        <style>
  61.            .center-image{margin: 0 auto 10px;} .alert{padding-left: 10px; margin-bottom: 5px; margin-top: 5px; }
  62.        </style>
  63.        <div class="col-sm-4 col-sm-offset-4 text-center">
  64.            <form action="' . esc_url($_SERVER['REQUEST_URI']) . '" method="post">
  65.                <a href="'.esc_url(get_site_url()).'">
  66.                        <img class="img-responsive center-image" src="'.get_template_directory_uri().'/images/logo.png" alt="" width="180">
  67.                </a>
  68.                <p class="lead">'.__("Register New Account","themeum").'</p>
  69.                <div class="form-group">
  70.                    <input type="text" autocomplete="off" class="required form-control"  placeholder="'.__("Username *","themeum").'" name="username" value="' . (isset($_POST['username']) ? $username : null) . '">
  71.                </div>
  72.                <div class="form-group">
  73.                    <input type="password" class="required form-control"  placeholder="'.__("Password *","themeum").'" name="password" value="' . (isset($_POST['password']) ? $password : null) . '">
  74.                </div>
  75.                <div class="form-group">
  76.                    <input type="text" autocomplete="off" class="required form-control" placeholder="'.__("Email *","themeum").'" name="email" value="' . (isset($_POST['email']) ? $email : null) . '">
  77.                </div>
  78.                <div class="form-group">
  79.                    <input type="text" autocomplete="off" class="form-control" placeholder="'.__("Website","themeum").'" name="website" value="' . (isset($_POST['website']) ? $website : null) . '">
  80.                </div>
  81.                <div class="form-group">
  82.                    <input type="text" autocomplete="off" class="form-control" placeholder="'.__("First Name","themeum").'" name="fname" value="' . (isset($_POST['fname']) ? $first_name : null) . '">
  83.                </div>
  84.                <div class="form-group">
  85.                    <input type="text" autocomplete="off" class="form-control" placeholder="'.__("Last Name","themeum").'" name="lname" value="' . (isset($_POST['lname']) ? $last_name : null) . '">
  86.                </div>
  87.                <div class="form-group">
  88.                    <input type="text" autocomplete="off" class="form-control" placeholder="'.__("Nickname","themeum").'" name="nickname" value="' . (isset($_POST['nickname']) ? $nickname : null) . '">
  89.                </div>
  90.                <div class="form-group">
  91.                    <textarea autocomplete="off" style="resize:none" class="form-control" placeholder="'.__("About / Bio","themeum").'" name="bio">' . (isset($_POST['bio']) ? $bio : null) . '</textarea>
  92.                </div>
  93.                <div class="form-group">
  94.                    <input type="submit" class="btn btn-success btn-lg btn-block" name="submit" value="'.__("Register","themeum").'"/>
  95.                </div>
  96.                <p>'.__("Already have an account?","themeum").' <a href="'.esc_url(get_permalink( get_option('login_page_id') )).'">'.__("Sign In","themeum").'</a></p>
  97.            </form>
  98.        </div>';
  99.     }
  100.  
  101.  
  102.  
  103.     function registration_validation( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio )  {
  104.         global $reg_errors;
  105.         $reg_errors = new WP_Error;
  106.  
  107.         if ( empty( $username ) || empty( $password ) || empty( $email ) ) {
  108.             $reg_errors->add('field', __('Required form field is missing','themeum'));
  109.         }
  110.  
  111.         if ( strlen( $username ) < 4 ) {
  112.             $reg_errors->add('username_length', __('Username too short. At least 4 characters is required','themeum'));
  113.         }
  114.  
  115.         if ( username_exists( $username ) )
  116.             $reg_errors->add('user_name', __('Sorry, that username already exists!','themeum'));
  117.  
  118.         if ( !validate_username( $username ) ) {
  119.             $reg_errors->add('username_invalid', __('Sorry, the username you entered is not valid','themeum'));
  120.         }
  121.  
  122.         if ( strlen( $password ) < 5 ) {
  123.             $reg_errors->add('password', __('Password length must be greater than 5','themeum'));
  124.         }
  125.  
  126.         if ( !is_email( $email ) ) {
  127.             $reg_errors->add('email_invalid', __('Email is not valid','themeum'));
  128.         }
  129.  
  130.         if ( email_exists( $email ) ) {
  131.             $reg_errors->add('email', __('Email Already in use','themeum'));
  132.         }
  133.        
  134.         if ( !empty( $website ) ) {
  135.             if ( !filter_var($website, FILTER_VALIDATE_URL) ) {
  136.                 $reg_errors->add('website', __('Website is not a valid URL','themeum'));
  137.             }
  138.         }
  139.  
  140.         if ( is_wp_error( $reg_errors ) ) {
  141.  
  142.             foreach ( $reg_errors->get_error_messages() as $error ) {
  143.                 echo '<div class="col-sm-4 col-sm-offset-4 text-center"><div class="alert alert-danger" role="alert"><strong>'.__('ERROR','themeum').'</strong>:'.$error.'</div></div>';
  144.             }
  145.         }
  146.     }
  147.  
  148.  
  149.  
  150.  
  151. function complete_registration() {
  152.     global $reg_errors, $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;
  153.     if ( count($reg_errors->get_error_messages()) < 1 ) {
  154.         $userdata = array(
  155.         'user_login'    =>  $username,
  156.         'user_email'    =>  $email,
  157.         'user_pass'     =>  $password,
  158.         'user_url'      =>  $website,
  159.         'first_name'    =>  $first_name,
  160.         'last_name'     =>  $last_name,
  161.         'nickname'      =>  $nickname,
  162.         'description'   =>  $bio,
  163.         );
  164.         $user = wp_insert_user( $userdata );
  165.         //On success
  166.         if ( ! is_wp_error( $user ) ) {
  167.             $u = new WP_User( $user );
  168.             $u->set_role( 'author' );
  169.         }
  170.         echo '<div class="col-sm-4 col-sm-offset-4 text-center"><div class="alert alert-success" role="alert">'.__("Registration complete.","themeum").' <a href="'.get_permalink( get_option('login_page_id') ).'">'.__("Sign In","themeum").'</a></div></div>';  
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. // Register a new shortcode: [custom_registration]
  177. add_shortcode('custom_registration', 'custom_registration_shortcode');
  178.  
  179. // The callback function that will replace [book]
  180. function custom_registration_shortcode() {
  181.     ob_start();
  182.     custom_registration_function();
  183.     return ob_get_clean();
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement