X

Redirect Based on User Roles or Capabilities

Snippets by IsItWP

You may want to add a redirect based on user roles or capabilities. It allows you to redirect users with higher user roles directly to the WordPress wp-admin.

Instructions: Add the following code to the functions.php file of your WordPress theme to add a redirect based on user roles and capabilities.

function wps_login_redirect_contributors() {
  if ( current_user_can('moderate_comments') ){
      return 'url-to-redirect-to';
  }
}

add_filter('login_redirect', 'wps_login_redirect_contributors');

You may also enjoy redirecting to the requested URL after successful login.

Comments  Leave a Reply

  1. Joerg Knoerchen June 29, 2017 at 8:16 am

    Hey, is there a way to redirect subscribers each time they try to access the profile at the dashboard (/wp-admin/profile.php)?

  2. I found this code will re-direct users after login (unless they’re an Admin):

    function baw_no_admin_access(){ if( !current_user_can( ‘administrator’ ) ) { wp_redirect( home_url() ); die(); }}add_action( ‘admin_init’, ‘baw_no_admin_access’, 1 );

    …however if you replace ‘administrator’ with ‘editor’, only editors can access the dashboard (even admins cannot).

    Do you have a snippet to allow both Admins AND editors to access the dashboard, but re-direct all other user roles?

    1. you could just do !current_user_can( ‘administrator’ ) || !current_user_can( ‘editor’ ) this would work with capabilities as well. Using || or would be fine,

  3. where to we have put the url of the page from where they’ll be redirected ?

    1. This does not going into your page, place this snippet into the functions.php of your wordpress theme. Then when a user who is a contributor logs in they will be sent to the URL you define.

  4. Doesn’t work for me..not even called the function..

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!
[60% OFF Today!]
[60% OFF Today!]
[10% OFF Today!]
[10% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]