Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
}
@loretoparisi

This comment has been minimized.

Copy link

commented Jul 22, 2018

Thanks a lot for this. Just few notes. When in a real plugin it's worth to call these functions like

// custom user fields
add_action( 'user_new_form', array( $this,'crf_admin_registration_form' ) );
add_action( 'user_profile_update_errors', array( $this,'crf_user_profile_update_errors'), 10, 3 );
add_action( 'edit_user_created_user', array( $this,'crf_user_register') );
add_action( 'show_user_profile', array( $this,'crf_show_extra_profile_fields') );
add_action( 'edit_user_profile', array( $this,'crf_show_extra_profile_fields') );

Specifically take care of the 2 parameters of add_action( 'user_profile_update_errors', array( $this,'crf_user_profile_update_errors'), 10, 3 );, since it's a common error to write it down in the array...

Another question. How to make the custom fields editable in the User Profile page? So far the crf_show_extra_profile_fields is only showing the fields, but I cannot edit them in place.

@malaspigar

This comment has been minimized.

Copy link

commented Sep 4, 2018

Thanks for your code! it was a saver. Please I have a request.

I want to 3 more fields, that is Phone number, Gender and Address. Please any idea how to go about this? I tried adding it though but the result was zero (0) in the backend view.
I will be grateful for your support.
Thanks again

@Gedeias

This comment has been minimized.

Copy link

commented Oct 31, 2018

@malaspigar
Any news? i too want 3 more fields. But to was zero. :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.