Created
October 23, 2017 21:20
-
-
Save anastis/ca224eecc6b24ee593f80c52e3afd9b1 to your computer and use it in GitHub Desktop.
WordPress registration fields. Tutorial code for https://www.cssigniter.com/how-to-add-custom-fields-to-the-wordpress-registration-form/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Custom Registration Fields | |
Plugin URI: | |
Description: | |
Version: 0.1 | |
Author: CSSIgniter | |
Author URI: | |
License: GPLv2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
/** | |
* Front end registration | |
*/ | |
add_action( 'register_form', 'crf_registration_form' ); | |
function crf_registration_form() { | |
$year = ! empty( $_POST['year_of_birth'] ) ? intval( $_POST['year_of_birth'] ) : ''; | |
?> | |
<p> | |
<label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ) ?><br/> | |
<input type="number" | |
min="1900" | |
max="2017" | |
step="1" | |
id="year_of_birth" | |
name="year_of_birth" | |
value="<?php echo esc_attr( $year ); ?>" | |
class="input" | |
/> | |
</label> | |
</p> | |
<?php | |
} | |
add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 ); | |
function crf_registration_errors( $errors, $sanitized_user_login, $user_email ) { | |
if ( empty( $_POST['year_of_birth'] ) ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: Please enter your year of birth.', 'crf' ) ); | |
} | |
if ( ! empty( $_POST['year_of_birth'] ) && intval( $_POST['year_of_birth'] ) < 1900 ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: You must be born after 1900.', 'crf' ) ); | |
} | |
return $errors; | |
} | |
add_action( 'user_register', 'crf_user_register' ); | |
function crf_user_register( $user_id ) { | |
if ( ! empty( $_POST['year_of_birth'] ) ) { | |
update_user_meta( $user_id, 'year_of_birth', intval( $_POST['year_of_birth'] ) ); | |
} | |
} | |
/** | |
* Back end registration | |
*/ | |
add_action( 'user_new_form', 'crf_admin_registration_form' ); | |
function crf_admin_registration_form( $operation ) { | |
if ( 'add-new-user' !== $operation ) { | |
// $operation may also be 'add-existing-user' | |
return; | |
} | |
$year = ! empty( $_POST['year_of_birth'] ) ? intval( $_POST['year_of_birth'] ) : ''; | |
?> | |
<h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th> | |
<td> | |
<input type="number" | |
min="1900" | |
max="2017" | |
step="1" | |
id="year_of_birth" | |
name="year_of_birth" | |
value="<?php echo esc_attr( $year ); ?>" | |
class="regular-text" | |
/> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 ); | |
function crf_user_profile_update_errors( $errors, $update, $user ) { | |
if ( $update ) { | |
return; | |
} | |
if ( empty( $_POST['year_of_birth'] ) ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: Please enter your year of birth.', 'crf' ) ); | |
} | |
if ( ! empty( $_POST['year_of_birth'] ) && intval( $_POST['year_of_birth'] ) < 1900 ) { | |
$errors->add( 'year_of_birth_error', __( '<strong>ERROR</strong>: You must be born after 1900.', 'crf' ) ); | |
} | |
} | |
add_action( 'edit_user_created_user', 'crf_user_register' ); | |
/** | |
* Back end display | |
*/ | |
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' ); | |
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' ); | |
function crf_show_extra_profile_fields( $user ) { | |
?> | |
<h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label></th> | |
<td><?php echo esc_html( get_the_author_meta( 'year_of_birth', $user->ID ) ); ?></td> | |
</tr> | |
</table> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
You have to play a little with HTML and use something like this in your HTML form
1with all the fields you need.
Greetings,
Francisco