$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Configuring WordPress Auth Cookie Expiration|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Configuring WordPress Auth Cookie Expiration

matteradmin7PV0评论

I'm trying to configure the WordPress cookie expiration time, but for some reason it's not working. I went here:

/

And put the following hook based on the doc:

function init() {
    // ...
    $expiration = 60;
    apply_filters( 'auth_cookie_expiration', $expiration );
}

This code is called from a hook in my plugin's constructor:

add_action( 'init', array( $this, 'init' ) );

And I have verified that it runs.

I expected this to produce a cookie that expires in 60 seconds, but it doesn't. Instead it sets a regular expiration time of 48 hours (WP default). I suspect this might be because when the user actually logs in, it doesn't create this expiration date, and running this function later has no effect. I haven't yet figured out how to make it work though. Any help appreciated.

I'm trying to configure the WordPress cookie expiration time, but for some reason it's not working. I went here:

https://developer.wordpress/reference/hooks/auth_cookie_expiration/

And put the following hook based on the doc:

function init() {
    // ...
    $expiration = 60;
    apply_filters( 'auth_cookie_expiration', $expiration );
}

This code is called from a hook in my plugin's constructor:

add_action( 'init', array( $this, 'init' ) );

And I have verified that it runs.

I expected this to produce a cookie that expires in 60 seconds, but it doesn't. Instead it sets a regular expiration time of 48 hours (WP default). I suspect this might be because when the user actually logs in, it doesn't create this expiration date, and running this function later has no effect. I haven't yet figured out how to make it work though. Any help appreciated.

Share Improve this question asked Jan 3, 2018 at 14:00 YnhockeyYnhockey 1932 silver badges8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Note that you're not adding any filter callback with:

apply_filters( 'auth_cookie_expiration', $expiration );

Instead use:

add_filter( 'auth_cookie_expiration', $callback, 10, 3 );

where $callback is the appropriate filter callback that modifies the expiration.

Here's an example

add_filter( 'auth_cookie_expiration', function( $length, $user_id, $remember ) {
    return $length; // adjust this to your needs.
}, 10, 3 );

or to fit with your current class setup:

add_filter( 'auth_cookie_expiration', [ $this, 'set_auth_cookie_expiration' ], 10, 3 );

where set_auth_cookie_expiration() is the appropriate method, e.g.:

public function set_auth_cookie_expiration ( $length, $user_id, $remember ) 
{
    return $length; // adjust this to your needs.
}
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );

function keep_me_logged_in_for_1_year( $expirein ) {
    return 31556926; // 1 year in seconds
}
Post a comment

comment list (0)

  1. No comments so far