Skip to content

Instantly share code, notes, and snippets.

@max-kk
Last active December 30, 2021 12:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-kk/ce4d0dad8222b9eb3606c76cb00db62c to your computer and use it in GitHub Desktop.
Save max-kk/ce4d0dad8222b9eb3606c76cb00db62c to your computer and use it in GitHub Desktop.
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
}
@cheyenneIT
Copy link

Hi there, I'm new to modal.
I have a question, where to place this code?
Does it belong in the functions.php?
Or do I have to save it under the given name? In this case, which folder would be the right one?

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment