I’ve a strange behaviour with the filter login_redirect and the action template_redirect.

I visit a page with a course grid and if I’m not logged-in, I’ll be redirected to the auth page (wp-login.php). The action hook template_redirect was fired.

Now I type my login into the form and submit it. Now the filter login_redirect is fired and if I’m not an admin, I’ll be redirected to the course grid page, not to the dashboard.

Here is, where the strange thing happen. I don’t see the course grid page, I’ll be redirected again to the auth page (wp-login.php). At this point I run into a loop.

If I debug the output of the function is_user_logged_in(), it returns a false.

But if I browse to the course grid page, after the first login, I’m logged-in. I can see the courses … So the login seems to work, but I don’t know why I’m redirected everytime again to the auth page.

Do I use the wrong actions or filters?

function tspb_secure_bp_sites() {
    global $post;

    if ( ! is_user_logged_in() && get_option( 'tk_course_grid_page', 0 ) == $post->ID ) {

add_action( 'wp', 'tspb_secure_bp_sites' );

function tspb_login_redirect( $redirect_to, $request, $user ) {
    if ( is_null( $user->roles ) ) {
        return $redirect_to;

    $grid_page = get_option( 'tk_course_grid_page', null );

    // Redirect to dashboard, if admin
    if ( in_array( 'administrator', $user->roles ) ) {
        return $redirect_to;

    // Redirect to home url, if course overview is not set
    if ( empty( $grid_page ) || get_post_status( $grid_page ) == false ) {
        return home_url();

    return get_permalink( $grid_page );

add_filter( 'login_redirect', 'tspb_login_redirect', 99, 3 );

Read more here: wordpress login_redirect and template_redirect generates a loop


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: