0

Problem: After login Wp redirects to either wp-admin or home (front-page.php) and not back to current page

I want the user to return to where he/she came from.

Ok, so this is what I've got:

I have a custom page ( page-about.php ) with bootstrap tabs (I only mention this in case it induces these problems).

The tabs are built using partials stored in template-parts and of course custom queries.

Some of these tabs contains content that should not be publically accessible. To hide it from non-logged-in users I have simply done the following:

<!-- template-parts/board.php -->

<div class="inner-wrapper">

   <?php
   global $post;

  // Calling the query
   $firstPage = new WP_Query(array(
      'post_type' => 'page',
      'pagename' => 'board',
      'page_id' => 317
   ));

  // Checking if user is logged in
   if (is_user_logged_in()) {
      while ($firstPage->have_posts()) : $firstPage->the_post();
   ?>

    <!-- A bunch of html in between here -->
  
      <?php endwhile;
      wp_reset_postdata();
     
     // If user is not logged in, the text below is shown asking user to log in.
   } else { ?>

      <p>You need to be logged in in order to view this content. Would you like to  <a href="<?php echo esc_url(wp_login_url()); ?>"><?php _e('log in') ?></a> now?</p>

   <?php } ?>
</div>

Concerning what to put inside the link fetching the wp_login_url(), I've also tried using

<?php

// Saving the current url in a var
// Hovering the link gives me: https://localhost:3000/wp-login.php?redirect_to=https%3A%2F%2Fmysite.local%2Fabout
$current_url = home_url( add_query_arg( [], $GLOBALS['wp']->request ) ); ?> 

<p>You need to be logged in in order to view this content. Would you like to <a href="<?php echo esc_url(wp_login_url(site_url(add_query_arg(array(), $wp->request)))); ?>" alt="<?php esc_attr_e('login', 'textdomain'); ?>"><?php _e('log in', 'textdomain'); ?></a> now?</p>

However I'm still being redirected to wp-admin.

I read that this may not be the optimal way of handling redirects because of how and when Wordpress tends to call the affected functions, so I also tried adding these to function.php (not at the same time obviously):

<?php
// #1. found on developer.wordpress.org -> login_redirect
function my_login_redirect($redirect_to, $request, $user)
{
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {
        //check for admins
        if (in_array('administrator', $user->roles)) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

// #2. A modified version of the above found here on stackexchange or stackoverflow
add_filter('login_redirect', 'my_login_redirect', 10, 3);

function my_login_redirect($redirect_to, $requested_redirect_to, $user)
{
    if (isset($user->roles) && is_array($user->roles)) {

        if (in_array('subscriber', $user->roles)) {

            if ($requested_redirect_to && admin_url() != $requested_redirect_to) {
                $redirect_to = $requested_redirect_to;
            } else {
                $redirect_to =  home_url();
            }
        }
    }
    return $redirect_to;
}
add_filter('login_redirect', 'my_login_redirect', 10, 3);
?>

So my main question is: How do I force Wordpress to redirect the user (Subscriber role) to the same page as before logging in using a custom page template and custom queries? None of the above seem to apply.

Thank you and sorry for this WOT!

1 Answer 1

0

Since you're inside the loop when calling the wp_login_url() you can just pass the get_the_permalink() as the $redirect url.

<a href="<?php echo wp_login_url( get_the_permalink() ); ?>">
   <?php echo __( 'Login' ); ?>
</a>
1
  • At first I did not see the connection here but after reading up on wp_login_url() things cleared even for me :) Thank you man!
    – Seblito
    Commented Jul 3, 2021 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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