Now a days many Websites and Web application are using WordPress as their back-end. With increasing users of WordPress requires many more functionality to implement. Many Web Application / Websites requires to redirect users based on User’s role to particular or custom page.
Today Here I will let you know how can we implement to redirect user by user’s role in your WordPress Website. We will use hook “wp_login” and add one function to check current user role and then based on role we will redirect the user to predefined page (Custom Page).
To implement this you need to create one plugin and add below code to that
<?php /* Plugin Name: My Login Redirect By Role Plugin URI: Description: Redirects users based on their role Version: 1.0 Author: TheDigiLife Author URI: http://thedigilife.com License: GPLv2 or later */ add_action('wp_login','my_redirect_user_by_role', 10, 3); function my_redirect_user_by_role($user_login, $user) { global $user; echo $role_name = $user->roles[0]; // redirection for subscriber if ( 'subscriber' === $role_name ) { wp_redirect( 'http://localhost/wordpress/?p=39'); exit; } // redirect for editor if ( 'editor' === $role_name ) { wp_redirect('http://yourdomain.com/path/to/page-post'); exit; } // you can add more roles if conditions here wp_redirect( 'http://yourdomain.com/path/to/page-post'); exit; }
exit is used after wp-redirect so that it will not be overridden.
Please add your comment and queries below.