Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created January 20, 2016 13:35
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 dingo-d/b61a40e6af41a901dcd1 to your computer and use it in GitHub Desktop.
Save dingo-d/b61a40e6af41a901dcd1 to your computer and use it in GitHub Desktop.
Custom registration forms
/********* Custom Registration Form ***********/
add_action( 'register_form', 'travelnews_register_form' );
if (!function_exists('travelnews_register_form')) {
function travelnews_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
$last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : '';
?>
<p>
<label for="first_name"><?php esc_html_e( 'First Name', 'travelnews' ) ?><br />
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label>
</p>
<p>
<label for="last_name"><?php esc_html_e( 'Last Name', 'travelnews' ) ?><br />
<input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr( wp_unslash( $last_name ) ); ?>" size="25" /></label>
</p>
<?php
}
}
add_filter( 'registration_errors', 'travelnews_registration_errors', 10, 3 );
if (!function_exists('travelnews_registration_errors')) {
function travelnews_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', esc_html__( '<strong>ERROR</strong>: You must include a first name.', 'travelnews' ) );
}
if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
$errors->add( 'last_name_error', esc_html__( '<strong>ERROR</strong>: You must include a last name.', 'travelnews' ) );
}
return $errors;
}
}
add_action( 'user_register', 'travelnews_user_register' );
if (!function_exists('travelnews_user_register')) {
function travelnews_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
}
if ( ! empty( $_POST['last_name'] ) ) {
update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment