Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LinzardMac/7c61a6abb44206a3168f1e55966cb611 to your computer and use it in GitHub Desktop.
Save LinzardMac/7c61a6abb44206a3168f1e55966cb611 to your computer and use it in GitHub Desktop.
add_action( 'register_form', 'add_my_field' );
function add_my_field(){
$custom_field = ( ! empty( $_POST['custom_field'] ) ) ? trim( $_POST['custom_field'] ) : '';
echo '<label for="custom_field"'. _e( 'Custom Field Label =', 'textdomain' ) .'</label><br />
<input type="text" name="custom_field" id="custom_field" class="input" value="'. echo esc_attr( wp_unslash( $custom_field ) ) .'" size="25" />
';
}
add_filter( 'registration_errors', 'custom_field_sanitize', 10, 3 );
function custom_field_sanitize( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['custom_field'] ) || ! empty( $_POST['custom_field'] ) && trim( $_POST['custom_field'] ) == '' ) {
$errors->add( 'custom_field_error', __( '<strong>ERROR</strong>: You must include a value.', 'textdomain' ) );
}
return $errors;
}
add_action( 'user_register', 'custom_field_save' );
function custom_field_save( $user_id ) {
if ( ! empty( $_POST['custom_field'] ) ) {
update_user_meta( $user_id, 'custom_field', trim( $_POST['custom_field'] ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment