Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I add 'first name', 'last name', 'date of birth' and 'terms and conditions' to WordPress' register fields? My site is adult-themed and I would like to verify users' ages and have them agree to the terms before entering.

share|improve this question
add comment

1 Answer 1

Adding fields

At first adding some fields to the registering formular

add_action( 'register_form', 'extended_register_form', 10, 0 );

function extended_register_form() {

    // if an error occurs, we are here again and the
    // values that are entered in the formular are setup in the $_POST array
    $age = filter_input( INPUT_POST, 'age', FILTER_SANITIZE_NUMBER_INT );

    echo '<p>';
    echo '<label for="age">Your age:&&nbsp;</label>';
    printf( '<input type="text" id="age" name="age" size="2" value ="%s" />', $age );
    echo '</p>';

    echo '<p>';
    echo '<input type="checkbox" id="tos_check" name="tos_check" />';
    echo '<label for="tos_check">&nbsp;I agree to the terms of service</label>';
    echo '</p>';

}

Verifying, validating & sanitazion

Now we have to verify the users input. If one or more values does not match our criteria, we want to throw an error message. To do this, hook into the registration_errors and add one or more errors to the error object. If the error object is not empty, the registration will be stopped and the errors will be printed.

add_filter( 'registration_errors', 'show_errors', 10, 3 );

function show_errors( $errors, $login, $email ) {

    $tos_check = ( isset( $_POST['tos_check'] ) && true == $_POST['tos_check'] ) ?
        true : false;

    if ( false == $tos_check )
        $errors->add( 'termsnotaccepted', 'You have to agree to the TOS' );

    $age_check = filter_input( INPUT_POST, 'age', FILTER_SANITIZE_NUMBER_INT );

    if ( $age_check < 21 )
        $errors->add( 'toyoungerror', 'Sorry, you are to young' );

    return $errors;

}

Storing the data

At last hook in the user registering routine and check if the (verified and validated) values are set. If the values are present, store them in the users meta.

add_action( 'user_register', 'user_register', 10, 1 );

function user_register( $user_id ) {

    $data = array();

    $age = filter_input( INPUT_POST, 'age', FILTER_SANITIZE_NUMBER_INT );

    if ( ! empty( $age ) )
      $data['age'] = $age;

    update_user_meta( $user_id, 'choose-a-key', $data );

    return $user_id;

}

Summary

  1. Add your fields to the registering formular
  2. Verify, validate and sanitize the values. If a required value is not set (or not valid), add an error. This will stop the registration process and print the error message
  3. Store the needed values in the users meta
share|improve this answer
    
+1 Only valuable answer here. –  kaiser Aug 2 '13 at 10:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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