Adding Extra Profile Fields

Adding extra profile 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 user_profile_update_errors filter.

3. Save the new fields

You will save the new fields by creating a callback function for the profile_update 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 a company field to the profile form. This field will be required.

All of the following code should be added to theme-my-login-custom.php.

Add the new fields

function add_tml_profile_form_fields() {
	tml_add_form_field( 'profile', 'company', array(
		'type'     => 'text',
		'label'    => 'Company',
		'value'    => tml_get_request_value( 'company', 'post' ),
		'id'       => 'company',
		'priority' => 65,
	) );
}
add_action( 'init', 'add_tml_profile_form_fields' );

Validate the new fields

function validate_tml_profile_form_fields( $errors ) {
	if ( empty( $_POST['company'] ) ) {
		$errors->add( 'empty_company', '<strong>ERROR</strong>: Please enter your company name.' );
	}
	return $errors;
}
add_filter( 'user_profile_update_errors', 'validate_tml_profile_form_fields' );

Save the new fields

function save_tml_profile_form_fields( $user_id ) {
	if ( ! empty( $_POST['company'] ) ) {
		update_user_meta( $user_id, 'company', sanitize_text_field( $_POST['company'] ) );
	}
}
add_action( 'profile_update', 'save_tml_profile_form_fields' );