Advertisement
Guest User

register-wp

a guest
Feb 5th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.61 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.   Plugin Name: Custom Registration
  5.   Plugin URI: http://code.tutsplus.com
  6.   Description: Updates user rating based on number of posts.
  7.   Version: 1.0
  8.   Author: Agbonghama Collins
  9.   Author URI: http://tech4sky.com
  10.  */
  11.  
  12. function custom_registration_function() {
  13.     if (isset($_POST['submit'])) {
  14.         registration_validation(
  15.         $_POST['username'],
  16.         $_POST['password'],
  17.         $_POST['email']
  18.         );
  19.        
  20.         // sanitize user form input
  21.         global $username, $password, $email;
  22.         $username   =   sanitize_user($_POST['username']);
  23.         $password   =   esc_attr($_POST['password']);
  24.         $email      =   sanitize_email($_POST['email']);
  25.  
  26.         // call @function complete_registration to create the user
  27.         // only when no WP_error is found
  28.         complete_registration(
  29.         $username,
  30.         $password,
  31.         $email
  32.         );
  33.     }
  34.  
  35.     registration_form(
  36.         $username,
  37.         $password,
  38.         $email
  39.         );
  40. }
  41.  
  42. function registration_form( $username, $password, $email ) {
  43.     echo '
  44.    <style>
  45.     div {
  46.         margin-bottom:2px;
  47.     }
  48.    
  49.     input{
  50.         margin-bottom:4px;
  51.     }
  52.     </style>
  53.     ';
  54.  
  55.     echo '
  56.    <form action="' . $_SERVER['REQUEST_URI'] . '" method="post" class="needs-validation" novalidate>
  57.    
  58.    <div class="form-group input-icon-left m-b-10">
  59.        <i class="fa fa-user"></i>
  60.        <input type="text" name="username" class="form-control form-control-secondary" placeholder="Username" value="' . (isset($_POST['username']) ? $username : null) . '" required>
  61.        <small class="form-text">This field can not be blank.</small>
  62.    </div>
  63.    <div class="form-group input-icon-left m-b-10">
  64.        <i class="fa fa-envelope"></i>
  65.        <input type="email" name="email" class="form-control form-control-secondary"  placeholder="Email Address" value="' . (isset($_POST['email']) ? $email : null) . '" required>
  66.        <small class="form-text">This field can not be blank.</small>
  67.    </div>
  68.    <div class="divider"><span>Security</span></div>
  69.    <div class="form-group input-icon-left m-b-10">
  70.      <i class="fa fa-lock"></i>
  71.      <input type="password" name="password" class="form-control form-control-secondary" placeholder="Password" value="' . (isset($_POST['password']) ? $password : null) . '" required>
  72.      <small class="form-text">This field can not be blank.</small>
  73.    </div>
  74.    <div class="divider"><span>Terms of Service</span></div>
  75.    <div class="form-group">
  76.        <label class="custom-control custom-checkbox custom-checkbox-primary custom-checked">
  77.        <input type="checkbox" class="custom-control-input" checked="">
  78.        <span class="custom-control-indicator"></span>
  79.        <span class="custom-control-description">Subscribe to weekly newsletter</span>
  80.    </label>
  81.    <div class="form-group">
  82.        <label class="custom-control custom-checkbox custom-checkbox-primary">
  83.            <input type="checkbox" class="custom-control-input" required>
  84.            <span class="custom-control-indicator"></span>
  85.            <span class="custom-control-description">Accept <a href="#" data-toggle="modal" data-target="#terms">terms of service</a></span>
  86.        </label>
  87.    </div>
  88.    <div class="divider"><span>I am not a robot</span></div>
  89.    <div class="form-group g-recaptcha-outer">
  90.      <script src="https://www.google.com/recaptcha/api.js"></script>
  91.      <div class="g-recaptcha" data-sitekey="6Le5b0QUAAAAAHD5sgGsM2_cUYSAoyU6j3RREU4O"></div>
  92.    </div>
  93.    <button type="submit" name="submit" class="btn btn-primary m-t-10 btn-block">Complete Registration</button>
  94.     </form>
  95.     ';
  96. }
  97.  
  98. function registration_validation( $username, $password, $email )  {
  99.     global $reg_errors;
  100.     $reg_errors = new WP_Error;
  101.  
  102.     if ( empty( $username ) || empty( $password ) || empty( $email ) ) {
  103.         $reg_errors->add('field', 'Required form field is missing');
  104.     }
  105.  
  106.     if ( strlen( $username ) < 4 ) {
  107.         $reg_errors->add('username_length', 'Username too short. At least 4 characters is required');
  108.     }
  109.  
  110.     if ( username_exists( $username ) )
  111.         $reg_errors->add('user_name', 'Sorry, that username already exists!');
  112.  
  113.     if ( !validate_username( $username ) ) {
  114.         $reg_errors->add('username_invalid', 'Sorry, the username you entered is not valid');
  115.     }
  116.  
  117.     if ( strlen( $password ) < 6 ) {
  118.         $reg_errors->add('password', 'Password length must be greater than 6');
  119.     }
  120.  
  121.     if ( !is_email( $email ) ) {
  122.         $reg_errors->add('email_invalid', 'Email is not valid');
  123.     }
  124.  
  125.     if ( email_exists( $email ) ) {
  126.         $reg_errors->add('email', 'Email Already in use');
  127.     }
  128. }
  129.  
  130.     if ( is_wp_error( $reg_errors ) ) {
  131.  
  132.         foreach ( $reg_errors->get_error_messages() as $error ) {
  133.             echo '<div>';
  134.             echo '<strong>ERROR</strong>:';
  135.             echo $error . '<br/>';
  136.  
  137.             echo '</div>';
  138.         }
  139.     }
  140.  
  141. function complete_registration() {
  142.     global $reg_errors, $username, $password, $email;
  143.     if ( count($reg_errors->get_error_messages()) < 1 ) {
  144.         $userdata = array(
  145.         'user_login'    =>  $username,
  146.         'user_email'    =>  $email,
  147.         'user_pass'     =>  $password,
  148.         );
  149.         $user = wp_insert_user( $userdata );
  150.         echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';  
  151.     }
  152. }
  153.  
  154. // Register a new shortcode: [cr_custom_registration]
  155. add_shortcode('cr_custom_registration', 'custom_registration_shortcode');
  156.  
  157. // The callback function that will replace [book]
  158. function custom_registration_shortcode() {
  159.     ob_start();
  160.     custom_registration_function();
  161.     return ob_get_clean();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement