WordPress User Redirection After Social Login Based on Roles

2 min read

In this tutorial, I will be sharing a code snippet that will redirect users to a custom WordPress page or URL based on their assigned role after they successfully log in to your via ProfilePress social login.

Note: the code should be pasted to your theme’s functions.php file or your site-specific / dummy plugin.


add_filter('ppress_social_login_redirect', 'ppress_social_login_redirect_by_role', 10, 3);

function ppress_social_login_redirect_by_role($url, $social_network, $user_id)
{
    $a = get_user_by('id', $user_id);
    //retrieve the user roles
    $user_roles = $a->roles;

    /**
     * we'll redirect users with student role to https://xyz.com/student/
     * and those with teacher role to https://xyz.com/teacher/
     */
    if (in_array('student', $user_roles)) {
        $url = 'https://xyz.com/student/';
    } elseif (in_array('teacher', $user_roles)) {
        $url = 'https://xyz.com/teacher/';
    }

    return $url;
}

Code Explanation

What the code above does is, check the role of the user logging in. If the user is a student (i.e. has a “student” role), they get redirected to https://xyz.com/student/ and if a teacher (with “teacher” role), they are redirected to https://xyz.com/teacher/

To include more user role check and login redirection, create an additional `elseif` conditional block for them and include it in the above code.

If you have any pre-sale question, inquiring or contribution, please get in touch.

Create Paid Membership Websites in Minutes

Install ProfilePress today to get a modern, powerful WordPress membership & ecommerce website.