Creating an alternate registration form

Sometimes, you will have a need to create an alternate registration form for different user types. In order to do so, you will need to register a new action and a new form to handle this specific registration. The steps would be as follows.

1. Register the new action

You will use the function tml_register_action() to register the action. You may opt to create a new action handler if necessary, or utilize one of the action handlers already provided by the plugin.

2. Register the new form

You will use the function tml_register_form() to register the form, and tml_add_form_field() to add the fields. Alternatively, you can copy the default registration form and use tml_add_form_field() or tml_remove_form_field() as needed.

3. Validate 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.

4. Save new fields (optional)

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

Let's say we want to create a specific registration experience for a "client". For simplicity, we will use the method of copying the default registration form and modifying it to our needs. We will also use the default registration handler, taking advantage of some provided hooks for our customization needs.

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

Register the new action

function register_client_registration_action() {
	tml_register_action( 'client_registration', array(
		'title'              => 'Client Registration',
		'slug'               => 'client-registration',
		'callback'           => 'tml_registration_handler',
		'show_on_forms'      => false,
		'show_nav_menu_item' => ! is_user_logged_in(),
	) );
}
add_action( 'init', 'register_client_registration_action', 5 );

You'll notice we defined the slug property as "client-registration". This will be used in the URL to access your new action. In order to make this work, you must first visit the Theme My Login Settings > General page in your WP Dashboard to update your permalinks. You should now see your custom action in the "Slugs" section of this page, which shows you the link to your new action.

Register the new form

function register_client_registration_form() {
	// First, we'll register the form
	tml_register_form( 'client_registration', array(
		'action' => tml_get_action_url( 'client_registration' ),
	) );

	// Now we'll copy all fields from the default registration form
	foreach ( tml_get_form_fields( 'register' ) as $field ) {
		tml_add_form_field( 'client_registration', $field );
	}

	// Now we can add any custom fields
	tml_add_form_field( 'client_registration', '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( 'client_registration', 'last_name', array(
		'type'     => 'text',
		'label'    => 'Last Name',
		'value'    => tml_get_request_value( 'last_name', 'post' ),
		'id'       => 'last_name',
		'priority' => 15,
	) );
	tml_add_form_field( 'client_registration', 'company', array(
		'type'     => 'text',
		'label'    => 'Company',
		'value'    => tml_get_request_value( 'company', 'post' ),
		'id'       => 'company',
		'priority' => 15,
	) );
}
add_action( 'init', 'register_client_registration_form', 5 );

Validate new fields

function validate_client_registration_form_fields( $errors ) {
	// We only want this callback to fire if our custom action is being run
	if ( tml_is_action( 'client_registration' ) ) {
		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.' );
		}
		if ( empty( $_POST['company'] ) ) {
			$errors->add( 'empty_company', '<strong>ERROR</strong>: Please enter the name of your company.' );
		}
	}
	return $errors;
}
add_filter( 'registration_errors', 'validate_client_registration_form_fields' );

Save new fields

function save_client_registration_form_fields( $user_id ) {
	// We only want this callback to fire if our custom action is being run
	if ( tml_is_action( 'client_registration' ) ) {
		if ( ! empty( $_POST['first_name'] ) ) {
			update_user_meta( $user_id, 'first_name', sanitize_text_field( $_POST['first_name'] ) );
		}
		if ( ! empty( $_POST['last_name'] ) ) {
			update_user_meta( $user_id, 'last_name', sanitize_text_field( $_POST['last_name'] ) );
		}
		if ( ! empty( $_POST['company'] ) ) {
			update_user_meta( $user_id, 'company', sanitize_text_field( $_POST['company'] ) );
		}

		// Perhaps we want to set a custom role for this type of registration? Use this.
		// $user = get_user_by( 'id', $user_id );
		// $user->set_role( 'client' );
	}
}
add_action( 'user_register', 'save_client_registration_form_fields' );