wp_set_comment_cookies()
云策文档标注
概述
wp_set_comment_cookies() 函数用于为未认证评论者设置身份存储的 cookies,通常用于在评论处于审核状态时回忆该评论者的先前评论。
关键要点
- 函数接受三个参数:$comment(WP_Comment 对象,必需)、$user(WP_User 对象,必需,可能不存在)、$cookies_consent(布尔值,可选,默认 true)。
- 如果用户已存在或 $cookies_consent 为 false,则不设置 cookies 或移除现有 cookies。
- 通过 setcookie() 设置 comment_author_、comment_author_email_ 和 comment_author_url_ 的 cookies,使用 COOKIEHASH、COOKIEPATH、COOKIE_DOMAIN 等常量。
- 支持 comment_cookie_lifetime 过滤器来调整 cookie 生命周期,默认从 YEAR_IN_SECONDS 开始。
代码示例
function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) {
// 如果用户已存在,或用户选择不存储 cookies,则不设置 cookies。
if ( $user->exists() ) {
return;
}
if ( false === $cookies_consent ) {
// 移除任何现有 cookies。
$past = time() - YEAR_IN_SECONDS;
setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
return;
}
/**
* 过滤评论 cookie 的生命周期(秒)。
*
* @since 2.8.0
* @since 6.6.0 默认 $seconds 值从 30000000 改为 YEAR_IN_SECONDS。
*
* @param int $seconds 评论 cookie 生命周期。默认 YEAR_IN_SECONDS。
*/
$comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', YEAR_IN_SECONDS );
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
}注意事项
- 函数在 WordPress 3.4.0 中引入,4.9.6 版本添加了 $cookies_consent 参数。
- 使用前需确保相关常量(如 COOKIEHASH)已定义,并注意安全设置(如 $secure 变量)。
原文内容
Sets the cookies used to store an unauthenticated commentator’s identity. Typically used to recall previous comments by this commentator that are still held in moderation.
Parameters
$commentWP_Commentrequired-
Comment object.
$userWP_Userrequired-
Comment author’s user object. The user may not exist.
$cookies_consentbooloptional-
Comment author’s consent to store cookies.
Default:
true
Source
function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) {
// If the user already exists, or the user opted out of cookies, don't set cookies.
if ( $user->exists() ) {
return;
}
if ( false === $cookies_consent ) {
// Remove any existing cookies.
$past = time() - YEAR_IN_SECONDS;
setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
return;
}
/**
* Filters the lifetime of the comment cookie in seconds.
*
* @since 2.8.0
* @since 6.6.0 The default $seconds value changed from 30000000 to YEAR_IN_SECONDS.
*
* @param int $seconds Comment cookie lifetime. Default YEAR_IN_SECONDS.
*/
$comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', YEAR_IN_SECONDS );
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
}
Hooks
- apply_filters( ‘comment_cookie_lifetime’, int $seconds )
-
Filters the lifetime of the comment cookie in seconds.