I’m trying to create a function to logout WordPress users after a period of inactivity. The timed element is working as it should and redirecting users after a set period of time.
The problem is that once the PHP script is loaded (in code sample), I run into the following error: Fatal error: Call to undefined function wp_logout() in /var/www/html/wp-content/plugins/ion-wp-login-timeout/scripts/timed-logout.php on line 5
All the reference material I read tells me that wp_logout() should log out the user but instead there are errors. I have tried several methods, including adding an action. I do not want to redirect the user to a login screen.
Sample of code is below. The echo statements are in place purely for testing. Any help would be much appreciated.
<?php function logout_this_session() { //Logout Now wp_logout(); wp_die(); } echo 'This will be the logout script<br/><br/>'; $last_page = $_SERVER['HTTP_REFERER']; echo 'You came from: ' . $last_page; logout_this_session(); header( 'Location: ' . $last_page ); ?>
Advertisement
Answer
I finally figured out a solution as in code sample below. From the last stage of the process before logging out the user, I have a wpsessionexpired=true value posted back to the page the user was on. The same page is immediately refreshed after the user is logged out. I placed this within the main plugin file.
function logoutUser(){ if ( $_POST["wpsessionexpired"] == 'true' ){ wp_logout(); header("refresh:0.5;url=".$_SERVER['REQUEST_URI'].""); } } add_action('init', 'logoutUser');