When a user registers on your website using the User Management add-on, you may wish to redirect existing users by their role or other criteria.
This can be done using WS Form custom server-side form validation.
In this tutorial we show you how to redirect a user if their email address already exists and if their user role is administrator. This code can be easily changed to redirect by other user roles or capabilities. The script would typically reside in the functions.php
file of your theme folder (always use the child theme for custom configuration if your theme allows it).
Creating The Registration Form
To create a registration form:
- Click on Add New from the WS Form admin menu.
- Click on the User Management tab.
- Click on the Registration template.
WS Form will then create a registration form for you.
Creating The Filter
First we are going to use the following function to create a new filter in WordPress:
add_filter( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
You can learn more about this function by clicking here.
The tag we will use is a WS Form filter called:
wsf_submit_field_validate
The second parameter provides WordPress with the callback function through which the form validation parameters will flow.
The third parameter determines the priority, we can just leave that with a number of 10.
The fourth parameter defines how many parameters are passed to the callback function.
The complete statement looks something like this:
add_filter( 'wsf_submit_field_validate', 'wsf_submit_field_validate_callback', 10, 6 );
Creating The Callback Function
This callback function is called automatically by WS Form when the form is submitted for each field on your form. It runs prior to any of your form actions running.
Six parameters are passed to the callback function, but we’re only concerned with 3 of these for this tutorial:
$field_error_action_array
– This array contains any actions that should run when the server side validation completes. If no validation actions occur, this array is blank. This value is always returned back to WS Form by your callback function. We add an element to this array if we want WS Form to do something on the client-side, in this case, run a redirect action.
$field_id
– This is the field ID on your form that is being validated. We’ll be looking out for the ID of the email field on your form.
$field_value
– This is the field value submitted by the user.
Here is an example of the callback function:
// My callback function for tag wsf_submit_field_validate function wsf_submit_field_validate_callback( $field_error_action_array, $field_id, $field_value, $section_repeatable_index, $post_mode, $submit_object ) { // Settings $my_email_field_id = 123; $user_role = 'administrator'; $redirect_url = '/redirect-here/'; // Check field ID switch($field_id) { case $my_email_field_id : // Validate email address if( !filter_var( $field_value, FILTER_VALIDATE_EMAIL ) ) { return $field_error_action_array; } // Get user by email using the value of this field $user = get_user_by( 'email', $field_value ); // Check if user exists if( !$user ) { return $field_error_action_array; } // Check user role if( in_array( $user_role, $user->roles ) ) { $field_error_action_array[] = array( 'action' => 'redirect', 'url' => $redirect_url ); } break; } return $field_error_action_array; }
At the top of this function you will see three setting that you can change:
$my_email_field_id
– Set this to the ID of the email field on your form. Learn how to find the ID of a field.
$user_role
– Set this to the user role you would like to redirect for.
$redirect_url
– Set this to the URL you would like to redirect to.
As you can see, we return $field_error_action_array
back to WS Form at the end of the function after making changes to it.
This example does the following:
- Checks the field ID using a switch statement. You would change 123 to the field ID of your email field. We use a switch statement here in case you want to add other field validations in future.
- We validate the email address. This is just good practice! If the email address is not valid, we return out of the callback function. WS Form will the invalid this field anyway.
- We attempt to get the user by email address.
- If the user cannot be found, we return
$field_error_action_array
and exit the callback function. - If the user can be found, we check the roles of the user to see if one of them is administrator. If they are, we tell WS Form to run a redirect action to the specified URL by adding an element to
$field_error_action_array
. You can learn more about the available actions here.
- If the user cannot be found, we return
Putting It All Together
The final code you would add to your functions.php file can be found below:
// Add a filter for custom server-side validation add_filter( 'wsf_submit_field_validate', 'wsf_submit_field_validate_callback', 10, 6 ); // My callback function for tag wsf_submit_field_validate function wsf_submit_field_validate_callback( $field_error_action_array, $field_id, $field_value, $section_repeatable_index, $post_mode, $submit_object ) { // Settings $my_email_field_id = 123; $user_role = 'administrator'; $redirect_url = '/redirect-here/'; // Check field ID switch($field_id) { case $my_email_field_id : // Validate email address if( !filter_var( $field_value, FILTER_VALIDATE_EMAIL ) ) { return $field_error_action_array; } // Get user by email using the value of this field $user = get_user_by( 'email', $field_value ); // Check if user exists if( !$user ) { return $field_error_action_array; } // Check user role if( in_array( $user_role, $user->roles ) ) { $field_error_action_array[] = array( 'action' => 'redirect', 'url' => $redirect_url ); } break; } return $field_error_action_array; }