max-kk
11/7/2018 - 2:12 PM

Added a phone to the AJAX Login and Registration modal popup PRO

Added a phone to the AJAX Login and Registration modal popup PRO

<?php

add_action( 'register_form', 'lrm_custom_registration_form' );
function lrm_custom_registration_form() {
    $phone = ! empty( $_POST['phone'] ) ? sanitize_text_field( $_POST['phone'] ) : '';
    ?>
    <p>
        <label for="phone"><?php esc_html_e( 'Phone', 'cn' ) ?><br/>
            <input type="text"
                   id="phone"
                   name="phone"
                   value="<?php echo esc_attr( $phone ); ?>"
                   class="input"
            />
        </label>
    </p>
    <?php
}
add_filter( 'registration_errors', 'lrm_custom_registration_errors', 10, 3 );
function lrm_custom_registration_errors( $errors, $sanitized_user_login, $user_email ) {
    if ( empty( $_POST['phone'] ) ) {
        $errors->add( 'phone_error', __( '<strong>ERROR</strong>: Please enter your Phone.', 'cn' ) );
    }
    return $errors;
}

add_action( 'user_register', 'lrm_custom_user_register' );
function lrm_custom_user_register( $user_id ) {
    if ( ! empty( $_POST['phone'] ) ) {
        update_user_meta( $user_id, 'phone', sanitize_text_field($_POST['phone'] ) );
    }
}
/**
 * Back end registration
 */
add_action( 'user_new_form', 'lrm_custom_admin_registration_form' );

function lrm_custom_admin_registration_form( $operation ) {
    if ( 'add-new-user' !== $operation ) {
        // $operation may also be 'add-existing-user'
        return;
    }
    $phone = ! empty( $_POST['phone'] ) ? sanitize_text_field( $_POST['phone'] ) : '';
    ?>
    <h3><?php esc_html_e( 'Personal Information', 'cn' ); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="phone"><?php esc_html_e( 'Phone', 'cn' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'cn' ); ?></span></th>
            <td>
                <input type="text"
                       id="phone"
                       name="phone"
                       value="<?php echo esc_attr( $phone ); ?>"
                       class="regular-text"
                />
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'user_profile_update_errors', 'lrm_custom_user_profile_update_errors', 10, 3 );
function lrm_custom_user_profile_update_errors( $errors, $update, $user ) {
    if ( $update ) {
        return;
    }

    if ( empty( $_POST['phone'] ) ) {
        $errors->add( 'phone_error', __( '<strong>ERROR</strong>: Please enter your Phone.', 'cn' ) );
    }
}
add_action( 'edit_user_created_user', 'lrm_custom_user_register' );
/**
 * Back end display
 */
add_action( 'show_user_profile', 'lrm_custom_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'lrm_custom_show_extra_profile_fields' );
function lrm_custom_show_extra_profile_fields( $user ) {
    ?>
    <h3><?php esc_html_e( 'Personal Information', 'cn' ); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="phone"><?php esc_html_e( 'Phone', 'cn' ); ?></label></th>
            <td><?php echo esc_html( get_user_meta( $user->ID, 'phone', true ) ); ?></td>
        </tr>
    </table>
    <?php
}