0

I am trying to redirect users(subscribers) to a particular page based on the following conditions 1. (subscriber) user is logged on 2. specific page id is matched 3. wp user role = subscriber which is the default role set when a user is registered on the platform

The Challenge i am having is conditions 1 and 2 works but not 3.

Here is my code:

function add_login_check()
{
    if ( is_user_logged_in() && is_page(1865)) {
        wp_redirect('http://destredirectedpage.php');
        exit;
    }
}
add_action('wp', 'add_login_check');

Interesting enough when i tested the above code using administrator role it works as expected but with subscriber role i get redirected automatically to the subscriber profile page upon logon.

1

We can check the $request which is passed to our login_redirect filter-function using url_to_postid.

// redirect subscribers if logging in from specific page
function wpse381872_login_redirect( $redirect_to, $request, $user ) {

    // turn the request url into a post-id
    $request_id = url_to_postid( $request );

    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        // check for subscribers logging in via 1865
        if ( 1865 === request_id && in_array( 'subscriber', $user->roles ) ) {
        
            $redirect_to = 'http://destredirectedpage.php';
            
        }
    }

    return $redirect_to;
}

add_filter( 'login_redirect', 'wpse381872_login_redirect', 10, 3 );
1
  • will give that a try.. – john zuh Feb 9 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.