How to redirect different user role users to unique links

How to redirect different user role users to unique links?.
I want to redirect admin to a specific page, a subscriber to specific page something likewise.

  • Prathamesh Palve
    • Staff

    Hello Brad Clarke,

    Thank you for giving us time to reframe the code. Here is the code which should work for you.

    function my_login_redirect( $redirect_to, $request, $user, $obj ){

    if( ! is_user_logged_in() ) return $redirect_to;

    $user = wp_get_current_user();
    $allowed_roles = array( 'administrator', 'author', 'contributor' );

    // if( array_intersect( $allowed_roles, $user->roles ) ) {
    // $redirect_to = admin_url();
    // }
    if( in_array( 'administrator', $user->roles) ){
    $redirect_to = admin_url();
    }elseif( in_array( 'editor', $user->roles) ){
    $redirect_to = admin_url();
    }elseif( in_array( 'author', $user->roles) ){
    $redirect_to = admin_url();
    }elseif( in_array( 'contributor', $user->roles) ){
    $redirect_to = admin_url();
    }else {
    $redirect_to = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_ACCOUNT );
    }
    wp_safe_redirect( $redirect_to );
    exit;
    //return $redirect_to;

    }
    add_filter('ms_controller_frontend_login_redirect', 'my_login_redirect', 9999, 4 );

    You can define the URLs according to your choice here. Please let me know if that works for you.
    I'm looking forward to hearing from you.

    Regards,
    Prathamesh

  • Fabio Fava
    • WordPress DevOps

    Hey Brad Clarke

    In case you're (like me) not very code-wise, don't get desperate: there are options.

    If your project needs a beter Login/Register/Profile interface, then the way to go is Theme My Login. It's premium add-ons are very interesting, worth a chance.

    But in case you need a more powerfull Membership solution, then the way to go would be Paid Memberships Pro. The free version is really powerful, and the premium add-ons turns it into a membership powerplant. More than worth a try.

    Hope one or both of those helps you: I use them both on my site fabiofava.com and they play nice together!

    • Brad Clarke
      • Design Lord, Child of Thor

      Hey Fabio Fava ,

      Thanks for the suggestions. Unfortunately I've had prior plugin conflicts with Theme my login and I'm already running an alternative membership plugin. Additionally I'd like to stay away from adding more plugins as my site is already pretty plugin heavy.

      Thanks though!!

      Brad

  • Prathamesh Palve
    • Staff

    Heya Brad Clarke,

    Taking this further, please refer the additions in the code here so to understand the working more easily:

    function wpmudev_custom_login_redirect( $redirect_to, $request, $user, $obj ){

    if( ! is_user_logged_in() ) return $redirect_to;

    $user = wp_get_current_user();
    //$allowed_roles = array( 'administrator', 'author', 'contributor' );

    // if( array_intersect( $allowed_roles, $user->roles ) ) {
    // $redirect_to = admin_url();
    // }
    if( in_array( 'administrator', $user->roles) ){
    $redirect_to = admin_url();
    }elseif( in_array( 'editor', $user->roles) ){
    $redirect_to = admin_url();
    }elseif( in_array( 'author', $user->roles) ){
    $redirect_to = admin_url();
    }elseif( in_array( 'contributor', $user->roles) ){
    $redirect_to = admin_url();
    }elseif( in_array( 'subscriber', $user->roles) ){
    // $subscriber_page = 'http://xyz.com/something-likethis'; //static link
    // $subscriber_page = home_url( '/about' ); //static link, ex: your-site.com/about
    // $subscriber_page = get_page_by_path( 'about' ); // get page link by slug your-site.com/about
    $subscriber_page = get_page_by_path( 'hello-world', OBJECT, 'post' ); //get post link by slug your-site.com/hello-world
    if( ! $subscriber_page ){
    $redirect_to = admin_url();
    }else{
    if( is_a( $subscriber_page, 'WP_Post' ) ){
    $redirect_to = get_permalink( $subscriber_page );
    }
    }
    }else {
    $redirect_to = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_ACCOUNT );
    }

    wp_safe_redirect( $redirect_to );
    exit;
    //return $redirect_to;

    }
    add_filter('ms_controller_frontend_login_redirect', 'wpmudev_custom_login_redirect', 9999, 4 );

    I have added in multiple conditions to understand the working for your reference:
    Here are the specifics:
    If you want to redirect it on a static link:

    // $subscriber_page = 'http://xyz.com/something-likethis';//
    static link

    If you want to redirect it to a page on your site

    // $subscriber_page = home_url( '/about' ); //
    static link, ex: your-site.com/about

    If you wouild like to redirect by using a slug

    // $subscriber_page = get_page_by_path( 'about' ); //
    get page link by slug your-site.com/about

    Feel free to reply in the thread here if you have any doubts further or need any help.

    Regards,
    Prathamesh

  • Brad Clarke
    • Design Lord, Child of Thor

    Hey Prathamesh Palve ,

    I see the three options but still can't make any of them work. Can you reduce the code to only redirect subscribers to a page called test? by link, not slug.

    I think I need to start with the minimum so I can try to understand how it works.

    Thanks for the help and sorry for the inconvenience!!

    Brad

  • Brad Clarke
    • Design Lord, Child of Thor

    Hey Prathamesh Palve ,

    After 437 login redirect attempts I was finally able to get a redirect code to work:

    function my_login_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
    if( $user->has_cap( 'administrator')) {
    $url = admin_url();
    }elseif( $user->has_cap( 'subscriber')) {
    $url = home_url('/test/');
    } else {
    $url = home_url('/test2/');
    }
    }
    return $url;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3 );

    Can you explain what the difference in the last line means. 10, 3 vs 9999, 4 ?

    Thanks

    Brad

  • Prathamesh Palve
    • Staff

    Hello Brad Clarke,

    Can have a look here: https://developer.wordpress.org/reference/functions/add_filter/ for further understanding.

    (10, 3 or 9999, 4 ) So these can be explained as follows:

    $priority
    Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

    Default value: 10

    $accepted_args
    The number of arguments the function accepts.

    10 or 9999 is priority; 3 or 4 is $accepted_args

    An example from the above link:

    // Filter call.
    $value = apply_filters( 'hook', $value, $arg2, $arg3 );

    // Accepting zero/one arguments.
    function example_callback() {
    ...
    return 'some value';
    }
    add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.

    // Accepting two arguments (three possible).
    function example_callback( $value, $arg2 ) {
    ...
    return $maybe_modified_value;
    }
    add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.

    I hope this helps you. Feel free to reply in the thread if you have doubts further.

    Regards,
    Prathamesh

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.