For more technical users or to skip the the background explanation, skip directly to the code.

When it comes to building themes or plugins in WordPress, you rarely want administrators, editors, subscribers, and so on to anything other than the dashboard just after they’ve logged in.

After all, these authors are usually loggin in to write posts, edit their information, or generally manage their content.

But if you’re building a more advanced theme or an application, then allowing users to view the dashboard may be outside the scope of what you’re looking to allow.

As such, here’s an easy to redirect non-admin users to another page after logging into WordPress. Note that this post was updated on December 13, 2012 thanks to the recommendations in Simon’s comment.

Understanding The Functions

Before actually taking a look at the function for redirecting users, there are two key functions that you need to be understood:

1. Getting The Site URL

Straight form the Codex:

The site_url template tag retrieves the site url for the current site (where the WordPress core files reside) with the appropriate protocol, ‘https’ if is_ssl() and ‘http’ otherwise.

In order to redirect the user to the homepage after logging into the application, we need to be able to direct them to the proper location of the application.

Generally speaking, the process will work like this:

  • Direct administrators to the dashboard (or /wp-admin/) to be exact
  • Direct all other users to the homepage

To do this, we can use the site_url() function in the following way:

  • admin_url();
  • site_url();

Easy enough.

2. Roles and Capabilities

Next, in WordPress, users have roles and there are five distinct roles each of which grant a user certain capabilities within the application.

The roles are:

  1. Administrator
  2. Editor
  3. Author
  4. Contributor
  5. Subscriber

You can read more about the distinctions between each of these roles on the Roles and Capabilities Codex article.

Whenever a user logs into WordPress, there is a filter that we have available: login_redirect. This particular function is used to change the location to which the user is directed after logging into WordPress.

At this point, we’ve got everything we need to setup a function to redirect non-admin users after login.

How To Redirect Non Admin Users After Login

The algorithm for doing this is simple:

  • If the user is an administrator, continue to admin_url()
  • Otherwise, redirect to, say the site_url()

The login_redirect filter will pass three arguments to the function:

  1. $redirect_to
  2. $request
  3. $user

We’re primarily concerned with the third as we can take a look at its roles attribute to determine if it contains the administrator value.

With that said, here’s the first pass at creating the conditional:

if( is_array( $user->roles ) ) {
	if( in_array( 'administrator', $user->roles ) ) {
		admin_url();
	} else {
		site_url();
	}
}

We can easily consolidate this into a ternary operation and convert it into a function:

/**
 * Redirect non-admins to the homepage after logging into the site.
 *
 * @since 	1.0
 */
function acme_login_redirect( $redirect_to, $request, $user  ) {
	return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url();
}
add_filter( 'login_redirect', 'acme_login_redirect', 10, 3 );

And with that, we are now redirecting non-admin users to the website’s homepage rather then the dashboard.

Share:

Leave a Reply