Skip to content

The Digi Life

Learning Life

  • Home
  • WP-Plugins
    • WordPress SMS Plugin – WP-SendSMS
  • Opencart Extensions
    • Simple Age Verification PopUp
    • Shopping cart in Pop Up and Buy Now Button
    • Auto Add Reward Points
    • Options Mouse Hover ToolTip Help
    • Category Carousel of Product
    • Show Reward Points of Customer In Header
  • Hire Developer..!

Recent Posts

  • Things do to be a better Software Engineer
  • Handling conflicts within a team or with stakeholders
  • Interview Questions and Answers for hiring lead developer
  • Authorization in Domain-Driven Design and Clean Architecture
  • 10 things that you should learn in 2023 as a Software Engineer

Categories

  • Android
  • Business
  • C Programming
  • C Programs Example
  • CMS
  • Javascript
  • Linux
  • MySQL
  • Netbeans
  • Opencart
  • PHP
  • PHP Beginner to advance
  • SEO
  • Teamsite
  • Ubuntu
  • Wordpress

Subscribe

Please Subscribe Here to Get Updates


 

Tag: Login Redirect

WordPress Redirect User to Custom Page based on User Role

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.

 

 

Posted on November 18, 2013Categories PHP, WordpressTags Login Redirect, PHP, WordPress, WP LoginLeave a comment on WordPress Redirect User to Custom Page based on User Role
Proudly powered by WordPress