Advertisement
Guest User

Untitled

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