Skip to content

Instantly share code, notes, and snippets.

@max-kk
Last active September 11, 2019 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-kk/83c30c3e0be6d967d0c0e733e4e1a338 to your computer and use it in GitHub Desktop.
Save max-kk/83c30c3e0be6d967d0c0e733e4e1a338 to your computer and use it in GitHub Desktop.
lrm_save_custom_field
<?php
// ONLY IF you want the user login via phone-number
// COPY after
add_filter('lrm/login_info_filter', function ($info) {
$user_login = $info['user_login'];
// Optionally
// $user_login = str_replace(["+", "-", " "], '', $info['user_login']);
// Commnets this to allow phone duplicates
$users = get_users(array(
'meta_key' => 'phone',
'meta_value' => $user_login,
));
if ( $users ) {
$info['user_login'] = $users[0]->user_login;
}
return $info;
});
<?php
// How to add html: https://monosnap.com/file/2MVkjClgYE3W3U9cgPEjy4VXXCzXf2
// COPY AFTER
add_filter( 'registration_errors',function ( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['phone'] ) ) {
$errors->add( 'phone_error', __( '<strong>ERROR</strong>: Please enter your Phone.', 'LANG_DOMAIN' ) );
} else {
// Commnets this to allow phone duplicates
$users = get_users(array(
'meta_key' => 'phone',
'meta_value' => $_POST['phone'],
));
if ( $users ) {
$errors->add( 'phone_error', __( '<strong>ERROR</strong>: This Phone is already registered!', 'LANG_DOMAIN' ) );
}
}
return $errors;
}, 10, 3 );
add_action( 'user_register', function ( $user_id ) {
if ( ! empty( $_POST['phone'] ) ) {
update_user_meta( $user_id, 'phone', sanitize_text_field($_POST['phone'] ) );
}
});
<?php
// COPY AFTER
/**
* Back end display
*/
add_action( 'show_user_profile', 'lrm_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'lrm_show_extra_profile_fields' );
function lrm_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', 'LANG_DOMAIN' ); ?></label></th>
<td><?php echo esc_html( get_user_meta( $user->ID, 'phone', true ) ); ?></td>
</tr>
</table>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment