Redirect Users by Role After LoginPRO

When a user logs in to your website using the User Management add-on, you may wish to redirect users by their role or other criteria.

This can be done using the WS Form Run WordPress Hook action.

In this tutorial we show you how to redirect a user if their 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 Login Form

To create a login form:

  1. Click on Add New from the WS Form admin menu.
  2. Click on the User Management tab.
  3. Click on the Login template.

WS Form will then create a login form for you.

Creating The Action

The next thing we need to do, is create an action on your form that will call the WordPress filter after the login occurs. To do this:

  1. Click the Actions  icon at the top of the page. The Action sidebar will open.
  2. Click the Add  icon to add a new action.
  3. Select Run WordPress Hook from the Action pulldown. A series of settings will appear.

Complete the settings as follows:

WS Form - Actions - Run WordPress Hook - Filter

Type: Choose Filter

Hook Tag: Enter your choice of a hook tag name. For this tutorial we’ll use: wsf_redirect_after_login

Priority: Choose After other actions

Creating The Filter

Next we are going to add some code to functions.php to handle the call to the hook. We’re 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 first parameter is the tag name which for this tutorial is:

wsf_redirect_after_login

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 WordPress Run WordPress Hook action passes two parameters.

The complete statement looks something like this:

add_filter( 'wsf_redirect_after_login', 'wsf_redirect_after_login_callback', 10, 2 );

Creating The Callback Function

This callback function will be called when WS Form runs the Run WordPress Hook filter.

Two parameters are passed to the callback function, but because we are only checking the logged in user we do not need to worry about those. For reference, however, they are:

$form_object – The form data.

$submit_object – The submission data.

Here is an example of the callback function:

// My callback function for tag wsf_redirect_after_login
function wsf_redirect_after_login_callback( $form_object, $submit_object ) {

	// Settings
	$user_role = 'administrator';
	$redirect_url = '/local-site/wp-admin/';

	// Check user is loged in
	if( !is_user_logged_in() ) {
		return;
	}

	// Get current user
	$user = wp_get_current_user();

	// Check user role
	if( in_array( $user_role, $user->roles ) ) {

		return array(

			'redirect' => array(

				'url' => $redirect_url
			)
		);
	}
}

At the top of this function you will see two setting that you can change:

$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 an array back to WS Form if the user has a role of administrator. The array tells WS Form that we want to do a redirect on the client side after the hook has run.

This example does the following:

  1. Checks to ensure the user is logged in. If they are not, we return out fo the function.
  2. We get the current user object using the WordPress function wp_get_current_user().
  3. We check the users roles and if they have a role of administrator, we return an array back to WS Form. The array tells WS Form that we want to do a redirect on the client side after the hook has run.

Putting It All Together

The final code you would add to your functions.php file can be found below:

// Add a filter for redirecting the user after login
add_filter( 'wsf_redirect_after_login', 'wsf_redirect_after_login_callback', 10, 2 );

// My callback function for tag wsf_redirect_after_login
function wsf_redirect_after_login_callback( $form_object, $submit_object ) {

	// Settings
	$user_role = 'administrator';
	$redirect_url = '/local-site/wp-admin/';

	// Check user is loged in
	if( !is_user_logged_in() ) {
		return;
	}

	// Get current user
	$user = wp_get_current_user();

	// Check user role
	if( in_array( $user_role, $user->roles ) ) {

		return array(

			'redirect' => array(

				'url' => $redirect_url
			)
		);
	}
}