Question

I am using the following code to add a first name and last name box to my signup page in wordpress. It is added to my themes function.php file and works fine, HOWEVER I'd like to add them to the top of the existing form - not the bottom. Can anyone help me with a way to do this please.

/*------- add first name to signup form -----------*/

//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:47%; font-size: 24px; margin:5px 5px 0px 0px; display:inline-block; float:left; position:relative;}
</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">' . __('First Name') . '</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" />';

// last_name
$last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
    echo '<label for="last_name">' . __('Last Name') . '</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" />';
}

//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.','') );
}
}

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;
}
Was it helpful?

Solution

If you use the hook signup_hidden_fields *, they show at the top, but $errors is not available:

add_action( 'signup_hidden_fields', 'full_name_register_form' );

function full_name_register_form ()
{
    // first_name
    $first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
        echo '<label for="first_name">' . __('First Name') . '</label>';
    
    echo '<input name="first_name" type="text" id="first_name" value="'. esc_attr($first_name) .'" maxlength="60" />';

    // last_name
    $last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
        echo '<label for="last_name">' . __('Last Name') . '</label>';
    
    echo '<input name="last_name" type="text" id="last_name" value="'. esc_attr($last_name) .'" maxlength="60" />';
}

* Inspect that file for other do_actions, there are a few others but it all depends on your setup.

Check this too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top