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?
wp_login_url(get_permalink())
...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.$redirect
does exactly that.?loggedin=true
but you could use anything for the same result