Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Call 'wp_logout' when cookie expires (php or javascript)

I am looking for a way to call wp_logout (server side) if my wordpress_logged_in_HASH cookie expires. Unfortunalty I can't find any solution so far. Did a lot of research on stackoverflow and google, but still not done. Does anyone have some advices or ideas how to do it?

Server side I can check if the cookie exits with $_SERVER['HTTP_COOKIE']. The variable exits on my shared server and this works. But how to run wp_logout automaticly when cookie expires? Maybe by some javascript?

if(strpos($_SERVER['HTTP_COOKIE'], 'wordpress_logged_in') == true){
    echo "Login cookie exists!<br>";
    var_dump($_SERVER['HTTP_COOKIE']);
    //echo($startTime = microtime(true));
} else{
    echo "Login cookie not exists!";
    var_dump($_SERVER['HTTP_COOKIE']);
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Simply change session expire time, as mentioned, by adding in your theme's functions.php something like:

add_filter('auth_cookie_expiration', 'my_expiration_filter', 99, 3);
function my_expiration_filter($seconds, $user_id, $remember){

    //if "remember me" is checked;
    if ( $remember ) {
        //WP defaults to 2 weeks;
        $expiration = 14*24*60*60; //UPDATE HERE;
    } else {
        //WP defaults to 48 hrs/2 days;
        $expiration = 2*24*60*60; //UPDATE HERE;
    }

    //http://en.wikipedia.org/wiki/Year_2038_problem
    if ( PHP_INT_MAX - time() < $expiration ) {
        //Fix to a little bit earlier!
        $expiration =  PHP_INT_MAX - time() - 5;
    }

    return $expiration;
}

Surprisingly enough, WordPress does not use PHP sessions at all. It uses only cookies. And it uses a number of them with hashed names.

So don't mess with them directly, and do it the WordPress way with filters like above.

Or by calling WordPress APIs. E.g. if you want to log out the current user, you can call wp_clear_auth_cookie: http://codex.wordpress.org/Function_Reference/wp_clear_auth_cookie

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!