admin管理员组文章数量:1130349
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 badges2 Answers
Reset to default 1Note 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
}
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 badges2 Answers
Reset to default 1Note 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
}
本文标签: plugin developmentConfiguring WordPress Auth Cookie Expiration
版权声明:本文标题:plugin development - Configuring WordPress Auth Cookie Expiration 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749151363a2323876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论