Reputation: 197
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 );
?>
Upvotes: 3
Views: 22107
Reputation: 1026
Adding to Hamed Moodi; Here
Create a custom function that logs out users
/**
* Log the current user out.
*
* @since 2.5.0
*/
function my_custom_user_logout() {
wp_destroy_current_session();
wp_clear_auth_cookie();
/**
* Fires after a user is logged-out.
*
* @since 1.5.0
*/
do_action( 'wp_logout' );
}
Then create a custom redirect after logout
add_action('init', 'mfsl_logout');
function mfsl_logout(){
if ( $_POST["wpsessionexpired"] == 'true' ){
wp_logout();
wp_safe_redirect( home_url() ); // custom URL to redirect after logot
exit();
}
}
When logs out they will be redirected to the home page or to any custom URL
Upvotes: 1
Reputation: 41
this is also working for
<?php
if (current_user_can('client') OR current_user_can('salesrep') ){
?>
<a href="<?php echo wp_logout_url( home_url() ); ?>" class="btn btn-primary"
style="float: right;" >Logout</a>
<?php }
?>
you can set any role as your need. like is this example (client, salesrep)
Upvotes: 0
Reputation: 11
Logout function according user role (this is a working code) copy and paste in function.php file
function redirect_after_logout() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if($role_name == 'subscriber'){
$redirect_url = site_url();
wp_safe_redirect( $redirect_url );
exit;
}
}
add_action( 'wp_logout', 'redirect_after_logout' );
Upvotes: 0
Reputation: 197
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');
Upvotes: 5
Reputation: 331
You can make your function inside functions.php
add_action( 'wp_logout', 'redirect_after_logout');
function redirect_after_logout(){
wp_redirect( 'http://example.com' );
exit();
}
Then put this in your desire position of your site:
<a href="<?php echo wp_logout_url(); ?>" >Logout</a>
Upvotes: 3
Reputation: 36
This must use in init action.
You can use wp_logout() functions that logout current user.
the details of this function in wp-includes/pluggable.php file:
<?php
/**
* Log the current user out.
*
* @since 2.5.0
*/
function wp_logout() {
wp_destroy_current_session();
wp_clear_auth_cookie();
/**
* Fires after a user is logged-out.
*
* @since 1.5.0
*/
do_action( 'wp_logout' );
}
Upvotes: 2