0

I used these code in my theme's functions.php to logout a particular user with user id 233 from everywhere in WordPress and it worked. But I want to logout all the users on my site from everywhere.

// worked for user with user id 233
$sessions = WP_Session_Tokens::get_instance($user_id=233);
$sessions->destroy_all();

I used this for all the users but did not work

// The code below did not work for all users
$sessions= WP_Session_Tokens::get_instance($user_id=users - get_current_user_id());
$sessions->destroy_all();
3

Firstly if you'd just like everyone to have to log in again you can void all of your cookies at once by changing your cookie hashes in your wordpress config file.

The lines that look like this:

define('AUTH_KEY',         ':u(rb=Ys8*2x^rVisqX(9=10a*.euPdYEBo6p_ZBB2CDG|rv~Ju)sh+6@=L6p_:l');
define('SECURE_AUTH_KEY',  '-`<iUz(4olJ`m&}1kA*{v&OX;Kq?W+!7ppF!tWb}cM<R=d<s=(oL]]04%AA@juZB');
define('LOGGED_IN_KEY',    '$KA]84n3ZT}v6m7c:r+!6l}R||)<_$3GxJ%F6Y?MopvxydJ@PRcmZ0-Hyq(EuEZt');
define('NONCE_KEY',        'qT=]!^BARWEMUD(G6]#~q$PAv-N[#^uE991w7HJNN<*mXavH)@o0TkFKVFUNIDN#');
define('AUTH_SALT',        'c W#7TWk{L1+n0A+^:|FHYL#-;*#<8U :v%>~S!45$bbYM09Iwe;@=uk^xeJd_D`');
define('SECURE_AUTH_SALT', 'h3&dgYcQE0uTaxd5bX$&1wC]yKP^?W8#q#>IJ*)ggf_Rc+6f+oZa?#JVS[HmO?=O');
define('LOGGED_IN_SALT',   '_L:n)>u>-31YK_ftTp_ZV>-y)qG)so|sOoWgYC9Ju!q_!%f.60g?I~]~lNZ6&o!0');
define('NONCE_SALT',       '.KI_*}ONxh/uwZv~!qYqOW+OL;?qvgaEqayN8>i+B.WY$e}+M9.Xqxwlf+?v 1k=');

This website is a good tool for generating new secure keys to put in.

If you want to change the amount of time you can be logged in for you can use the below code in your themes functions.php:

add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for' );

function keep_me_logged_in_for( $expirein ) {
    return 86400; // time in seconds
}
  • @ Bysander. I added this but did not log the user out after 300 seconds /* * Clear all user's cookies / $sessions = WP_Session_Tokens::get_instance( get_current_user_id() ); $sessions->destroy_all(); / * Cookies expiration time */ add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for' ); function keep_me_logged_in_for( $expirein ) { return 300; // time in seconds } – user2969052 Oct 10 '16 at 16:57
0

Try this:

$sessions = WP_Session_Tokens::get_instance( get_current_user_id() );
$sessions->destroy_all();

I don't think you have to set the variable inside of the brackets, you just need to pass the ID of the user (which is done for you by get_current_user_id function).

Here's a reference to the get_instance function, in case you haven't read it.

  • @ Dmitriy Demir, it worked. Thank you. Am trying to add time to it, so that every 24 hours the cookies will be cleared. – user2969052 Oct 10 '16 at 11:18
  • The code provided by Bysander seems to be correct. I can't comment there (don't have enough reputation), so I'll put my suggestion here - perhaps you tried it on an account that was already logged in? I think this code would just change the cookie expiration time when they are set, but it won't change the expiration time for cookies that have been set already. So try logging out, use Bysander code and then log in and see if it logs you out after 300 seconds. And you would probably need to refresh the page after 300 seconds to see the changes. – Dmitriy Gamolin Oct 11 '16 at 8:05
  • This is correct - the expiration time is set on the cookie. If you put a time of 0 the cookie should expire on the close of the browser window (end of session) - although this is unpredictable. This would work the same for 300 seconds and so you would need to close / quit your browser to see the effects. To do a "live" logout or timeout (like banks) you would require something else. As far as I'm aware the admin side of WP will take into account when the expiration is and ask you to log in again if it's been over the threshold time without activity. – Bysander Oct 11 '16 at 11:54
  • @ Dmitriy Demir, @ Bysander. Thank you, it worked. – user2969052 Oct 12 '16 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.