By: Prem Tiwari | Last Updated: | In: Code Snippets, WordPress Snippets
Hi friends, I hope you doing good. In this tutorial, I am going to share very usefull WordPress code snippets which will help you during new site development using WordPress. Please keep your close attention in this post as I am going to share how to redirect the user after login in WordPress based on their role on the site.
How to Redirect user after login in WordPress
Use the below WordPress snippets inside wp-content/themes/your-theme-folder/functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php function redirect_user_on_role() { //retrieve current user info global $current_user; get_currentuserinfo(); //If login user role is Subscriber if ($current_user->user_level == 0) { wp_redirect( home_url() ); exit; } //If login user role is Contributor else if ($current_user->user_level > 1) { wp_redirect( home_url() ); exit; } //If login user role is Editor else if ($current_user->user_level >8) { wp_redirect( home_url() ); exit; } // For other rolse else { $redirect_to = 'http://google.com/'; return $redirect_to; } } add_action('admin_init','redirect_user_on_role'); ?> |
I hope this post help you! Please do like and share this tutorial with your friends and don’t forget to subscribe to get all future tutorials dyractly in your email box.
Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies. View all posts by Prem Tiwari
Tags: wordpress login, wordpress tutorial, wordpress tutorial for beginner, wordpress tutorial for beginners, wordpress tutorials, wordpress tutorials for beginners, wordpress tutorils, wp snippets