Skip to content

Instantly share code, notes, and snippets.

@max-kk
Last active August 26, 2020 15:39
Show Gist options
  • Save max-kk/4a3afc5e16bbeecfaf6314d66c7a28a2 to your computer and use it in GitHub Desktop.
Save max-kk/4a3afc5e16bbeecfaf6314d66c7a28a2 to your computer and use it in GitHub Desktop.
LRM :: save custom fields
<?php
// How to add html: https://monosnap.com/file/2MVkjClgYE3W3U9cgPEjy4VXXCzXf2
// COPY AFTER
$lrm_fields = [ 'billing_city'=>'City', 'billing_state'=>'State', 'billing_postcode' => 'Postcode', 'ETC'=>'ETC' ];
add_filter( 'registration_errors',function ( $errors, $sanitized_user_login, $user_email ) use($lrm_fields) {
foreach( $lrm_fields as $lrm_field=>$lrm_field_label ) {
if ( empty( $_POST[$lrm_field ] ) ) {
$errors->add( $lrm_field .'_error', '<strong>ERROR</strong>: Please enter your ' . $lrm_field_label . '.' );
}
}
return $errors;
}, 10, 3 );
add_action( 'user_register', function ( $user_id ) use($lrm_fields) {
foreach( $lrm_fields as $lrm_field=>$lrm_field_label ) {
if ( ! empty( $_POST[$lrm_field] ) ) {
update_user_meta( $user_id, $lrm_field, sanitize_text_field($_POST[$lrm_field] ) );
}
}
});
<?php
// How to add html: https://monosnap.com/file/2MVkjClgYE3W3U9cgPEjy4VXXCzXf2
// COPY AFTER
$lrm_fields = [ 'phone'=>'Phone', 'address'=>'Address' ];
add_filter( 'registration_errors',function ( $errors, $sanitized_user_login, $user_email ) use($lrm_fields) {
foreach( $lrm_fields as $lrm_field=>$lrm_field_label ) {
if ( empty( $_POST[$lrm_field ] ) ) {
$errors->add( $lrm_field .'_error', '<strong>ERROR</strong>: Please enter your ' . $lrm_field_label . '.' );
}
}
return $errors;
}, 10, 3 );
add_action( 'user_register', function ( $user_id ) use($lrm_fields) {
foreach( $lrm_fields as $lrm_field=>$lrm_field_label ) {
if ( ! empty( $_POST[$lrm_field] ) ) {
update_user_meta( $user_id, $lrm_field, sanitize_text_field($_POST[$lrm_field] ) );
}
}
});
@priyankrawat
Copy link

How to send custom fields as an email to user and admin?

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