Creating a function to logout a WordPress user?

wordpress logout user by id
wordpress logout link without confirmation
wordpress logout redirect to current page
user login function in wordpress
change logout url - wordpress
wordpress logout menu shortcode
force logout user wordpress
wordpress custom logout url

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 );

?>

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' );
}

Creating a function to logout a WordPress user?, How to automatically logout inactive or idle users in WordPress You can enter the time in minutes and make sure it is not too short or too long. message field, you will find more plugin options to change logout functionality. We Provide Comprehensive Tools To Millions Of Users. 24/7 Support. Powering Over 2 Million Websites Worldwide

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>

wp_logout() | Function, To create a login/logout link in WordPress you can use a snippet of php code using the wp_logout_url function, you can also set the logout URL  Smart, Powerful, Responsive Theme. Get Divi + 87 more themes only $89!

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');

wp_logout_url() | Function, It's not as simple as calling wp_logout before any content is sent, for this code, a logout request redirects to the login page, with a GET add_action('init', ' check_logout'); function check_logout() { if(!isset($_GET['logout'])  Wordpress Site. Get Useful Information In Seconds. Visit Today & Quickly Get More Results On Fastquicksearch.com

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'  );

How to Automatically Log out Idle Users in WordPress, Learn how to log out users from WordPress using a shortcode generated to create a logout link or URL which which clicked or opened, log out users from your Note: this differs from the first method in that it does not display an HTML link  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

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)

How to create a login/logout link for WordPress, Methods to Force Log Out All WordPress Site Users the second method also it is not advisable to make any sudden change on user's logged  If you want to create a Logout button, or a link the user can click to log out, use wp_logout_url() instead. — By Carike — 7 months ago Please do not copy / paste this code into a button if you are a new user.

How do you log a user out with wp_logout?, For those instances, you may need to force logout all users in WordPress. First, I'll show you the manual method, then a quick and easy plugin method. The plugin you chose to use to create your membership or pay-per-view website  To create a login/logout link in WordPress you can use a snippet of php code using the wp_logout_url function, you can also set the logout URL to be an external site to your own.

How to Logout Users from WordPress Using a Custom Link, That being said, let’s take a look at how to easily logout all users in WordPress. Force Log Out All WordPress Users. For this tutorial, you will be editing WP Config file called wp-config.php. We highly recommend that you backup your wp-config.php file before making any changes to it.

How to Force Logout All Users from WordPress Site, There are two different methods for creating a custom WordPress user role. The first is by adding some code to a custom plugin or your theme’s functions.php file. The second is by installing one of the many available plugins that enable you to create and edit roles. The method you utilize really depends on personal preference.

Comments