Skip to content

Instantly share code, notes, and snippets.

@ecelis
Last active August 29, 2015 14:16
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 ecelis/eda5ab3c080c69614198 to your computer and use it in GitHub Desktop.
Save ecelis/eda5ab3c080c69614198 to your computer and use it in GitHub Desktop.
Custom wordpress registration form
<?php
/*
* Plugin Name: My Registration
* mkdir wp-content/plugins/myreg
*/
// Add new form element
add_action('register_form', 'myreg_register_form');
function myreg_register_form() {
if(!empty($_POST['first_name'])) {
$first_name = trim($_POST['first_name']);
} else {
$first_name = '';
}
?>
<p>
<label for="first_name"><?php _e( 'First Name', 'mydomain' ) ?><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>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myreg_registration_errors', 10, 3 );
function myplugin_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', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
return $errors;
}
// 3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myreg_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment