Home / functions.php / Redirect based on user roles or capabilities
  • How to Redirect based on user roles or capabilities

    redirect-based-on-user-roles-or-capabilities

    Redirecting based on a user role or capabilities could be a valuable addition depending on the needs of your WordPress site. This would allow you to redirect specific capabilities to a custom page with details about new content or features. You could also use this to redirect users with higher user roles directly to the WordPress wp-admin. For more details on the capabilities available in wordpress visit the wordpress codex. To use this snippet just add it to the functions.php of your wordpress theme and you will be redirect users based on roles or capabilities like a pro.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function wps_login_redirect_contributors() {
      if ( current_user_can('moderate_comments') ){
          return 'url-to-redirect-to';
      }
    }
    add_filter('login_redirect', 'wps_login_redirect_contributors');
    

    ( WordPress codex functions, hooks, in this snippet. )

    login_redirect, current_user_can, user_can, add_filter, wp,
  • Tomas

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

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

    • 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.

  • 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?

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