钩子文档

post_password_expires

💡 云策文档标注

概述

post_password_expires 是一个 WordPress 过滤器,用于修改受密码保护文章的密码 Cookie 的过期时间。默认情况下,Cookie 在创建后 10 天过期,但可以通过此过滤器调整为其他时间或设置为会话 Cookie。

关键要点

  • 过滤器名称:post_password_expires
  • 默认过期时间:10 天(time() + 10 * DAY_IN_SECONDS)
  • 参数:$expires(整数,传递给 setcookie() 的过期时间)
  • 返回 0 可将 Cookie 设置为会话 Cookie(浏览器关闭时过期)
  • 引入版本:WordPress 3.7.0

代码示例

function Modif_expir_cookie( $time ) {
  return time() + 600;  // 10 分钟
  // 对于 5 分钟:
  // return time() + 300;  // 在此情况下 60 * 5
  // return 0;  // 设置 Cookie 在会话结束时过期
}
add_filter('post_password_expires', 'Modif_expir_cookie');

注意事项

使用此过滤器时,确保返回一个整数时间戳,以正确设置 Cookie 的过期时间。返回 0 会创建会话 Cookie,适用于需要更高安全性的场景。


📄 原文内容

Filters the life span of the post password cookie.

Description

By default, the cookie expires 10 days from creation. To turn this into a session cookie, return 0.

Parameters

$expiresint
The expiry time, as passed to setcookie().

Source

$expire = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );

Changelog

Version Description
3.7.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Allows you to limit the cookie validity time either to a value passed as a parameter or to the session itself.

    function Modif_expir_cookie( $time ) { 
      return time() + 600 ;  // 10 mn
      // for 5 minutes :  
      // return time() + 300;  in this case 60 * 5 
      // return 0; set cookie to expire at the end of the session
    }
    add_filter('post_password_expires', 'Modif_expir_cookie' );