Revision 366 is a pre-publication revision. (Viewing current revision instead.)

Redirect Non-Administrators After Login in WordPress

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: [php] if( is_array( $user->roles ) { if( in_array( 'administrator', $user->roles ) ) { admin_url(); } else { site_url(); } } [/php] Or even better: [php] /** * Redirect non-admins to the homepage after logging into the site. * * @since 1.0 */ function soi_login_redirect( $redirect_to, $request, $user ) { return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url(); } // end soi_login_redirect add_filter( 'login_redirect', 'soi_login_redirect', 10, 3 ); [/php]

Revisions

  • February 22, 2013 @ 05:49:54 [Current Revision] by PeterLugg
  • February 22, 2013 @ 05:45:19 by PeterLugg

Revision Differences

There are no differences between the February 22, 2013 @ 05:45:19 revision and the current revision. (Maybe only post meta information was changed.)

No comments yet.

Leave a Reply