2

I'm trying to change some of the content being shown to viewers based on whether they are logged in or not. I'm using the is_user_logged_in() function in my child theme's functions.php to replace the read more link like this:

if ( ! function_exists( 'x_excerpt_string' ) ) :
  function x_excerpt_string( $more ) {
    if (is_user_logged_in()) {
      return '  <div><a href="' . get_permalink() . '" class="more-link">' . __( 'Read More', '__x__' ) . '</a></div>';
    }
    else {
      return '<div><a class="more-link" href="' . wp_login_url(get_permalink()) . '">Log in to read more</a></div>';
    }
  }
  add_filter( 'excerpt_more', 'x_excerpt_string' );
endif;

I'm also using the function in template files to change the wp query like this:

if (is_user_logged_in()) {
    query_posts( 'orderby=rand&posts_per_page=1&post_type=card&cat=9' );
    }
else {
    query_posts( 'orderby=rand&posts_per_page=1&post_type=card&cat=8' );
}

and

<?php if (!is_user_logged_in()) { echo '
<p>To get access to the whole card deck along with follow-up exercices, please <a href="' . wp_login_url(get_permalink()) . '">log in.</a> If you are not a member, <a href="/members">register for free</a>.'; } ?>

In both cases, I have viewers reporting that they're seeing the wrong content immediately after logging in. They'll be redirected to the oririnal URL after logging in but it will show the logged out content until they refresh, at which point the function will do its job. It seems like the redirect shows the user's cached pages and doesn't check if the user logged in since their last visit, even with caching plugins turned off. Is there a way to change that?

6
  • How are you redirecting upon login? Commented Feb 23, 2017 at 23:55
  • The login links are set with wp_login_url(get_permalink()) Commented Feb 24, 2017 at 1:21
  • Doesn't answer my question. ...immediately after logging in - a user should normally see the admin dashboard after logging in, not theme template files. It stands to reason that the default logic is being modified somehow. Hence my question. Commented Feb 24, 2017 at 2:56
  • My understanding of codex.wordpress.org/Function_Reference/wp_login_url is that $redirect does exactly that. Commented Feb 24, 2017 at 4:24
  • 1
    I've fixed this a few time for clients. Every time it was due to a browser end cache reloading it's cached version on the first load after redirect, on refesh the server says 'this content has been modified so grab the new version'. Look at your cache settings see how hard your doing browser caching. You can work around this by adding a query paramiter to the redirect URL from login - that way it's a unique request in the browsers eyes. I used ?loggedin=true but you could use anything for the same result Commented Mar 1, 2017 at 17:39

0

Your Answer

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

Browse other questions tagged or ask your own question.