- This topic has 3 replies, 2 voices, and was last updated 10 years, 10 months ago by
Pie Register.
- AuthorPosts
- October 13, 2015 at 2:59 am #10004129
Noah MeyhinParticipantHello,
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
October 13, 2015 at 3:37 pm #10004144
Pie RegisterParticipantHello,
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
October 14, 2015 at 3:14 am #10004146
Noah MeyhinParticipantHello,
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
October 14, 2015 at 4:40 pm #10004147
Pie RegisterParticipantHello,
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
- AuthorPosts
- The forum ‘Pie-Register 2.0 Support’ is closed to new topics and replies.
