0

I'd like to reorder the fields in the default WP registration form. I'm adding additional fields with my plugin but they are appearing below the username and email fields. My code is below (it's essentially copied and modified from here).

Does anyone know how I can do that? I assume it involves adding a parameter to the action register_form, but I can't find anything in the codex about it. Also, if you have any advice on how I could improve validation as well (if necessary), it would be appreciated as well as I'm trying to learn how to build plugins properly.

// Set variables
$theme_name = 'ibm';

/* ****************************************************************** */
                    /* !ADD FORM ELEMENTS */
/* ****************************************************************** */ 

function osu_register_form() {
    // Get and set any values already sent
    $first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
    $last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: ''; ?>
    <p>
        <label for="first_name"><?php _e('First Name', $theme_name) ?><br />
        <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr(stripslashes($first_name)); ?>" size="25" /></label>
    </p>
    <p>
        <label for="last_name"><?php _e('Last Name', $theme_name) ?><br />
        <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr(stripslashes($last_name)); ?>" size="25" /></label>
    </p>
<?php }

/* ****************************************************************** */
                        /* !VALIDATION */
/* ****************************************************************** */ 

function osu_registration_errors($errors, $sanitized_user_login, $user_email) {
    if ( empty( $_POST['first_name'] ) ) {
        $errors->add( 'first_name_error', __('<strong>ERROR</strong>: You must include a first name.', $theme_name) );
    }
    if ( empty( $_POST['last_name'] ) ) {
        $errors->add( 'last_name_error', __('<strong>ERROR</strong>: You must include a last name.', $theme_name) );
    }
    return $errors;
}


/* ****************************************************************** */
                    /* !SAVE USER META DATA */
/* ****************************************************************** */ 

function osu_user_register($user_id) {
    if ( isset( $_POST['first_name'] ) ) {
        update_user_meta($user_id, 'first_name', $_POST['first_name']);
    }
    if ( isset( $_POST['last_name'] ) ) {
        update_user_meta($user_id, 'last_name', $_POST['last_name']);
    }
}


// Add functions to WP actions and filters
add_action('register_form','osu_register_form');
add_filter('registration_errors', 'osu_registration_errors', 10, 3);
add_action('user_register', 'osu_user_register');
4
  • I don't get the question. If the form is created by your plugin then you control what appears in it and in what order. Commented Nov 18, 2013 at 11:14
  • Hi Mark, sorry, I meant that this plugin adds additional fields to the standard Wordpress registration form, what I'd like to do is reorder the fields if possible. I've updated my questions to make things clearer.
    – Osu
    Commented Nov 18, 2013 at 12:07
  • 1
    Bad news. There isn't any hook to do so. you will have to keep the custom fields below default username and email. Commented Nov 18, 2013 at 14:20
  • Ah, that's frustrating. Thanks for clarifying Maruti, I'll have to make do with things as they are.
    – Osu
    Commented Nov 18, 2013 at 17:34

1 Answer 1

0

The default login form is not very developer friendly (and it is more developer friendly now than it used to be). If you browse the source for that page you should be able to convince yourself of that.

Your plugin is not going to be able to manipulate the page the way you hope. A theme could create its own login page, but even that doesn't actually eliminate the default page.

You could attempt to circumvent the login page and replace it via something as in this answer but as stated in that answer it seems prone to error as wp-login.php is hard-coded into Core source multiple times.

1
  • This seems like the best solution for a bad situation, thanks for adding the details s_ha_dum, I'll probably keep things as is then otherwise I'll try Pippin's approach or plugin which involves rebuilding the login page: pippinsplugins.com/…
    – Osu
    Commented Nov 18, 2013 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.