I am using the below redirect code in the functions.php
file.
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
$adfsurllogouturl = 'some url';
wp_redirect( $adfsurllogouturl );exit();
}
It's working fine. The issue is that once logged out, it redirects to SOMEURL
when I click on the back button of the browser from the redirected page, it shows previous page details.
But I want that it should go on the login page.
I used the below code to fix it but it's not working.
function check_if_user_is_loggedin_action()
{
if ( is_user_logged_in() )
{
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
}
add_action('init', 'check_if_user_is_loggedin_action');
Please suggest to me. Is it the correct way?
CodePudding user response:
To redirect users after successfull logout to the needed page, you can add the following code:
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_safe_redirect( '/login-page' );
exit();
}
With the following button code:
<a href="<?php echo wp_logout_url(); ?>" title="Logout">Logout</a>
Instead of /login-page
please, use the needed URL.
Upd.
According to the solution provided here: https://stackoverflow.com/a/40766644/17572092
You can use below code to check for session variable:
<?php
if(!isset($_SESSION['login'])) :
header("Location: login.php");
?>
When user logout destroy the session:
<?php
unset($_SESSION['login']);
session_destroy();
?>
CodePudding user response:
Try out this code:
<pre><code>
add_action(‘wp_logout’,’auto_redirect_after_logout’);
function auto_redirect_after_logout(){
wp_redirect( home_url() );
exit();
}
add_action( ‘check_admin_referer’, function($action, $result) {
if ( ‘log-out’ == $action && is_user_logged_in() && !empty($_GET[‘action’])
&& ‘logout’ == $_GET[‘action’] ) {
wp_logout();
wp_redirect( home_url() );
exit();
}
}
</code></pre>