WordPress Plugins

How To Redirect Users After Successful Login In WordPress?

redirect-users-after-login-wp (1)Redirecting users is a crucial part in user management while building and managing your traffic community around your website. You can redirect users to your blogs front page, to the backend and to another location. How will you redirect the users depend where do you want to redirect them and what sorts of activities the users perform on your website. Namely there are five roles like Subscriber, Contributor, Author, Editor and Administrator. The contributor and author roles are mostly performed by same users across different websites while Administrator and Editor can remain same in other instances. Here in this article we shall be describing a few ways of redirecting users to various locations of your choice.

How to redirect members without a role?

Use the function.php file to apply below code to assign specific role to users based on their login details. Below code checks an users contributor role and redirects him accordingly:

Functions members_login_redirect($redirect_to, $request, $user)

{

$role = ‘contributor’;

If(in_array($role, $user->roles))

{

Return ‘/contributors’;

}

}

Add_filter(‘login_redirect’, ‘members_login_redirect’, 10, 3);

How to redirect members with a specific role to specific location?

Now if you want to redirect the contributor user to a specific location then you should enter your URL inside the code, then the code will be slightly changed and will look like below:

{

If(in_array($role, $user->roles))

{

Return contributor_url();

}

}

If the member is not a contributor then you need to return him to a different location and you can do it simply by adding an URL with ‘else’ or assigning a different role to him. On such cases you need to add below code:

Else

{

Return ‘/members’;

}

}

Add_filter (‘login_redirect’, ‘users_login_redirect’, 10, 3);

How to redirect users to see a specific page content?

Those of us who monetize their websites often need the users to be managed to view specific url or pages. In such cases, the above code may not be much helpful. In such cases, more specifically in traffic diversion, you can try below code:

Function redirect_primary_site()

{

If (!site_admin())

{

Global $current_user;

Get_userdetails();

$blog_info = glob_info(get_usermeta($current_user>ID, ‘primary_site’));

If ($_server[‘HTTP_HOST’]!=$blog_info -> domain)

{

Header (‘URL: http://’.$blog_info->domain.’/wp-admin/();

}

}

}

Add_action(‘admin_init’, ‘redirect_primary_site’);

The above code is but descriptive and you can modify it per your need. Below are the smartest codes to do all the same thing without much coding job. See below:

$redirect_user = get_blogurl_by_id( $primary );

$redirect_to = $redirect_user;

} else

$redirect_to = the_url(‘user.php’);

How to redirect users with different role statuses to their specific pages?

This part is my favorite as I raised the issue in the beginning. What will you do if you have different user roles and statuses and each of them need to be redirected to right location? Here is the simplest thing that you can do:

Functions redirect_user_based_on_role()

{

Global $current_user;

Get_userdetails();

If ($current_user ->user_level=1)

{

Wp_redirect (homepage_url());

}

Else

If ($current_user ->user_level=2)

{

Wp_redirect (authorpage_url());

}

Else

If ($current_user ->user_level=3)

{

Wp_redirect (contributorpage_url());

}

Else

If ($current_user ->user_level=4)

{

Wp_redirect (editorpage_url());

Exit;

}

}

Add_action(‘admin_init’; ‘redirect_user_based_on_role”);

Can you redirect users on WordPress without coding?

Yes, you can, WordPress is the easiest of all CMS and it has bundles of free to use WordPress plugins that can make your work easier. Some free to use and popular plugins to redirect users on a WordPress site include followings:

User Access Manager: The User Access Manager is a popular redirecting plugin that you can use to separate access of reader and the writers. You can even define group access with the plugin. The multilingual plugin lets administrator hide posts and comments and lock page content and limit access. The User Access Manager integrates easily with WP versions starting from 3.0.

Profile Builder: The Profile Builder plugin is specially suitable to manage users on the frontend of the website and it is compatible with all versions of WP starting from 3.1. It lets users to register on the site, login, edit their profiles and recover their passwords in short time. Administrator can benefit from the plugin by assigning roles to users and customizing and controlling the access.

Peter’s Login Redirect: The Peter’s Login Redirect is probably the most relevant plugin we are talking here. It is multilingual and supports access control and user role specification. A brief on the plugins suitability can be found here.

Quick Page Post Redirect Plugin: The Quick Page Post Redirect plugin is the most popular one in this last that has crossed the mark of more than half million active download and activation. It complies easily with any WordPress versions and works seamlessly with administrator’s requirement. Bunch of its functional capabilities are available here so you can review easily.

The above codes and plugin descriptions are aimed at the beginner learners; depending on your WP administrator skills you can modify them further. Alternatively you can contact your hosting service provider to help you sort out the redirecting. However, using plugins will let you do it yourself without any hassle.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.