Adding Extra Registration Fields
Adding extra registration fields with Theme My Login is extremely simple. It can be accomplished using the following three simple steps.
1. Add the new fields
You will use the function tml_add_form_field() to add the fields.
2. Validate the new fields (optional)
If you wish to require a field or perform any other kind of validation, you will do so using the registration_errors filter.
3. Save the new fields
You will save the new fields by creating a callback function for the user_register action. Within this callback function, you will more than likely be using update_user_meta() to save the fields, depending on your situation and what fields you are trying to save.
Examples
The following is an example of adding first name and last name to the registration form. These fields will be required.
All of the following code should be added to theme-my-login-custom.php.
Add the new fields
function add_tml_registration_form_fields() { tml_add_form_field( 'register', 'first_name', array( 'type' => 'text', 'label' => 'First Name', 'value' => tml_get_request_value( 'first_name', 'post' ), 'id' => 'first_name', 'priority' => 15, ) ); tml_add_form_field( 'register', 'last_name', array( 'type' => 'text', 'label' => 'Last Name', 'value' => tml_get_request_value( 'last_name', 'post' ), 'id' => 'last_name', 'priority' => 15, ) ); } add_action( 'init', 'add_tml_registration_form_fields' );
Validate the new fields
function validate_tml_registration_form_fields( $errors ) { if ( empty( $_POST['first_name'] ) ) { $errors->add( 'empty_first_name', '<strong>ERROR</strong>: Please enter your first name.' ); } if ( empty( $_POST['last_name'] ) ) { $errors->add( 'empty_last_name', '<strong>ERROR</strong>: Please enter your last name.' ); } return $errors; } add_filter( 'registration_errors', 'validate_tml_registration_form_fields' );
Save the new fields
function save_tml_registration_form_fields( $user_id ) { if ( isset( $_POST['first_name'] ) ) { update_user_meta( $user_id, 'first_name', sanitize_text_field( $_POST['first_name'] ) ); } if ( isset( $_POST['last_name'] ) ) { update_user_meta( $user_id, 'last_name', sanitize_text_field( $_POST['last_name'] ) ); } } add_action( 'user_register', 'save_tml_registration_form_fields' );