I know how to direct logged out user to a particular page or a URL, like:
<a href="https://wordpress.stackexchange.com/questions/184731/<?php echo wp_logout_url( home_url() ); ?>">Log out</a>
But if I want the user to redirect to the URL where he/she just was, what can I do?
Though I’m not knowledgeable about wp_get_referer()
, but I tried:
<?php $redirect_to = wp_get_referer() ? wp_get_referer() : home_url(); ?>
<a href="https://wordpress.stackexchange.com/questions/184731/<?php echo wp_logout_url( $redirect_to ); ?>">Log out</a>
and
<?php $redirect_to = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : home_url(); ?>
<a href="https://wordpress.stackexchange.com/questions/184731/<?php echo wp_logout_url( $redirect_to ); ?>">Log out</a>
with no luck. 🙁
It’s always taking me to the home_url()
.