Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Custom Registration
- Plugin URI: http://code.tutsplus.com
- Description: Updates user rating based on number of posts.
- Version: 1.0
- Author: Agbonghama Collins
- Author URI: http://tech4sky.com
- */
- function custom_registration_function() {
- if (isset($_POST['submit'])) {
- registration_validation(
- $_POST['username'],
- $_POST['password'],
- $_POST['email']
- );
- // sanitize user form input
- global $username, $password, $email;
- $username = sanitize_user($_POST['username']);
- $password = esc_attr($_POST['password']);
- $email = sanitize_email($_POST['email']);
- // call @function complete_registration to create the user
- // only when no WP_error is found
- complete_registration(
- $username,
- $password,
- $email
- );
- }
- registration_form(
- $username,
- $password,
- $email
- );
- }
- function registration_form( $username, $password, $email ) {
- echo '
- <style>
- div {
- margin-bottom:2px;
- }
- input{
- margin-bottom:4px;
- }
- </style>
- ';
- echo '
- <form action="' . $_SERVER['REQUEST_URI'] . '" method="post" class="needs-validation" novalidate>
- <div class="form-group input-icon-left m-b-10">
- <i class="fa fa-user"></i>
- <input type="text" name="username" class="form-control form-control-secondary" placeholder="Username" value="' . (isset($_POST['username']) ? $username : null) . '" required>
- <small class="form-text">This field can not be blank.</small>
- </div>
- <div class="form-group input-icon-left m-b-10">
- <i class="fa fa-envelope"></i>
- <input type="email" name="email" class="form-control form-control-secondary" placeholder="Email Address" value="' . (isset($_POST['email']) ? $email : null) . '" required>
- <small class="form-text">This field can not be blank.</small>
- </div>
- <div class="divider"><span>Security</span></div>
- <div class="form-group input-icon-left m-b-10">
- <i class="fa fa-lock"></i>
- <input type="password" name="password" class="form-control form-control-secondary" placeholder="Password" value="' . (isset($_POST['password']) ? $password : null) . '" required>
- <small class="form-text">This field can not be blank.</small>
- </div>
- <div class="divider"><span>Terms of Service</span></div>
- <div class="form-group">
- <label class="custom-control custom-checkbox custom-checkbox-primary custom-checked">
- <input type="checkbox" class="custom-control-input" checked="">
- <span class="custom-control-indicator"></span>
- <span class="custom-control-description">Subscribe to weekly newsletter</span>
- </label>
- <div class="form-group">
- <label class="custom-control custom-checkbox custom-checkbox-primary">
- <input type="checkbox" class="custom-control-input" required>
- <span class="custom-control-indicator"></span>
- <span class="custom-control-description">Accept <a href="#" data-toggle="modal" data-target="#terms">terms of service</a></span>
- </label>
- </div>
- <div class="divider"><span>I am not a robot</span></div>
- <div class="form-group g-recaptcha-outer">
- <script src="https://www.google.com/recaptcha/api.js"></script>
- <div class="g-recaptcha" data-sitekey="6Le5b0QUAAAAAHD5sgGsM2_cUYSAoyU6j3RREU4O"></div>
- </div>
- <button type="submit" name="submit" class="btn btn-primary m-t-10 btn-block">Complete Registration</button>
- </form>
- ';
- }
- function registration_validation( $username, $password, $email ) {
- global $reg_errors;
- $reg_errors = new WP_Error;
- if ( empty( $username ) || empty( $password ) || empty( $email ) ) {
- $reg_errors->add('field', 'Required form field is missing');
- }
- if ( strlen( $username ) < 4 ) {
- $reg_errors->add('username_length', 'Username too short. At least 4 characters is required');
- }
- if ( username_exists( $username ) )
- $reg_errors->add('user_name', 'Sorry, that username already exists!');
- if ( !validate_username( $username ) ) {
- $reg_errors->add('username_invalid', 'Sorry, the username you entered is not valid');
- }
- if ( strlen( $password ) < 6 ) {
- $reg_errors->add('password', 'Password length must be greater than 6');
- }
- if ( !is_email( $email ) ) {
- $reg_errors->add('email_invalid', 'Email is not valid');
- }
- if ( email_exists( $email ) ) {
- $reg_errors->add('email', 'Email Already in use');
- }
- }
- if ( is_wp_error( $reg_errors ) ) {
- foreach ( $reg_errors->get_error_messages() as $error ) {
- echo '<div>';
- echo '<strong>ERROR</strong>:';
- echo $error . '<br/>';
- echo '</div>';
- }
- }
- function complete_registration() {
- global $reg_errors, $username, $password, $email;
- if ( count($reg_errors->get_error_messages()) < 1 ) {
- $userdata = array(
- 'user_login' => $username,
- 'user_email' => $email,
- 'user_pass' => $password,
- );
- $user = wp_insert_user( $userdata );
- echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';
- }
- }
- // Register a new shortcode: [cr_custom_registration]
- add_shortcode('cr_custom_registration', 'custom_registration_shortcode');
- // The callback function that will replace [book]
- function custom_registration_shortcode() {
- ob_start();
- custom_registration_function();
- return ob_get_clean();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement