Custom redirect after login based on user role

This topic contains 3 replies, has 2 voices, and was last updated by Avatar Pie Register 3 years, 9 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #10004129
    Avatar
    Noah Meyhin
    Participant

    Hello,

    is it possible to implement a custom redirect after login, based on user role ?

    The plugin “peter’s login redirect” would normally do that, but I can’t make it work with pie register…

    I’ve read several topics on this subject, but none came with a solution or a code snippet that might actually be working.

    I thank you in advance for your answer, I’m quite sure that I’m not the only one interested in custom redirects.

    Kind regards,

    Noah

    #10004144
    Avatar
    Pie Register
    Participant

    Hello,

    PR triggers WP_LOGIN hook on login along with its own login hook (piereg_admin_login_before_redirect_hook and piereg_user_login_before_redirect_hook). You can put your redirect funtion on eiher of them, here’s how you will do it.

    add_action(‘wp_login’,’myCustomLoginHook’, 10, 2);

    function myCustomLoginHook($username,$userObject){
    //Your Custom Redirect Snipet goes here.
    }

    Hope it helps.

    Thanks

    #10004146
    Avatar
    Noah Meyhin
    Participant

    Hello,

    thank you for your quick reply.

    Following your advice, I’ve tried to add this snippet, but apparently it doesn’t work :

    add_action(‘wp_login’,’myCustomLoginHook’, 10, 2);
    function myCustomLoginHook($username,$userObject){
    $role_name = $userObject->roles[0];
    if ( ‘aaa’ == $role_name ) {
    wp_redirect( ‘http://www.mysite.com/aaa’ );
    exit;
    }
    elseif ( ‘bbb’ == $role_name ) {
    wp_redirect( ‘http://www.mysite.com/bbb’ );
    exit;
    }
    elseif( ‘ccc’ == $role_name ) {
    wp_redirect( ‘http://www.mysite.com/ccc’ );
    exit;
    }
    }

    It doesn’t work better if I use the piereg_user_login_before_redirect_hook
    Do your have any idea of what the problem might be ?

    Thanks again for your precious help !

    Noah

    #10004147
    Avatar
    Pie Register
    Participant

    Hello,

    I tested the code and its working fine. Just make sure you have entered the correct user role and this code snippet placed inside your theme’s function.php.

    add_action('wp_login','myCustomLoginHook', 10, 2);
    function myCustomLoginHook($username,$userObject){
        $role_name = $userObject->roles[0];
            /*echo '<pre>';
            print_r($role_name);
            die('TESTING');*/
        if ( 'administrator' == $role_name ) {
          wp_redirect( 'http://example.com/sample-page/' );
              exit;
        } 
        elseif ( 'bbb' == $role_name ) {
          wp_redirect( 'http://www.mysite.com/bbb' );
              exit;
        }
        elseif( 'ccc' == $role_name ) {
          wp_redirect( 'http://www.mysite.com/ccc' );
              exit;
        } 
    }

    Thanks

Viewing 4 posts - 1 through 4 (of 4 total)

The forum ‘Pie-Register 2.0 Support’ is closed to new topics and replies.

Checkout
0
PURCHASE NOW