Web Development

WordPress Redirect User Based on Role After Login

We had a long queue of the wordpress articles in past few days. Here is one more article to add in that queue.

This article will show how to redirect user after login based on their roll just like admin, author, editor and subscriber.

So now without spending much time just have a look at this code block for this trick.

[cc lang=”php”]
function redirect_based_on_role()
{
//get current user info
global $current_user;
get_currentuserinfo();

if ($current_user->user_level == 0)
{
// User is subsriber
// Redirect to respective page
}
else if ($current_user->user_level > 1)
{
// User is contributor
// Redirect to respective page
}
else if ($current_user->user_level >8)
{
// User is editor
// Redirect to respective page
}
else
{
// No User role found
// Get out of here
}
}
// Using this action we can make this possible.
add_action(“admin_init”,”redirect_based_on_role”);
[/cc]

If you have looked at above code then you can see that we have used one of the available action of the wordpress. This action is [code]admin_init[/code].

In above code I have written that redirect to respective page, here you can use wp_redirect created by wordpress team to redirect to specific URL.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *