Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add forename and surname fields to wp-signup.php This is similar to adding fields to wp-login.php?action=register, but it works for multisite WordPress instead. For the original, see: https://codex.wordpress.org/Customizing_the_Registration_Form
<?php
/**
* Plugin Name: Register with Full Name
* Description: Adds forename and surname fields to the registration form
* Version: 0.1
* Author: Douglas Livingstone
*/
//0. Style the new form elements...
add_action( 'wp_head', 'full_name_signup_stylesheet' );
function full_name_signup_stylesheet() {
?>
<style type="text/css">
.mu_register #first_name,
.mu_register #last_name { width:100%; font-size: 24px; margin:5px 0; }
</style>
<?php
}
//1. Add a new form element...
add_action('signup_extra_fields','full_name_register_form');
function full_name_register_form ($errors)
{
// first_name
$first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
echo '<label for="first_name">' . __('Forename:') . '</label>';
if ( $errmsg = $errors->get_error_message('first_name') ) {
echo '<p class="error">'.$errmsg.'</p>';
}
echo '<input name="first_name" type="text" id="first_name" value="'. esc_attr($first_name) .'" maxlength="60" /><br />';
// last_name
$last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
echo '<label for="last_name">' . __('Surname:') . '</label>';
if ( $errmsg = $errors->get_error_message('last_name') ) {
echo '<p class="error">'.$errmsg.'</p>';
}
echo '<input name="last_name" type="text" id="last_name" value="'. esc_attr($last_name) .'" maxlength="60" /><br />';
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter('signup_user_init', 'full_name_signup_user_init', 10, 1);
function full_name_signup_user_init($arguments)
{
if ( isset( $_POST['signup_for'] ) ) {
// first_name
$errors = $arguments['errors'];
if ( empty( $_POST['first_name'] ) ) {
$errors->add( 'first_name', __('Please enter your first name.','') );
}
// last_name
$errors = $arguments['errors'];
if ( empty( $_POST['last_name'] ) ) {
$errors->add( 'last_name', __('Please enter your last name.','') );
}
}
return $arguments;
}
//3. Finally, save our extra registration user meta.
add_action('add_signup_meta', 'full_name_add_signup_meta');
function full_name_add_signup_meta ($meta)
{
// first_name
if ( isset( $_POST['first_name'] ) ) {
$meta['first_name'] = $_POST['first_name'];
}
// last_name
if ( isset( $_POST['last_name'] ) ) {
$meta['last_name'] = $_POST['last_name'];
}
return $meta;
}
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.