wp_set_comment_cookies

函数
wp_set_comment_cookies ( $comment, $user, $cookies_consent = true )
参数
  • (WP_Comment) $comment Comment object.
    Required:
  • (WP_User) $user Comment author's user object. The user may not exist.
    Required:
  • (bool) $cookies_consent Optional. Comment author's consent to store cookies. Default true.
    Required:
    Default: true
定义位置
相关方法
sanitize_comment_cookieswp_set_auth_cookiewp_set_comment_statuswp_list_commentswp_insert_comment
引入
3.4.0
弃用
-

wp_set_comment_cookies: 这是一个WordPress函数,用于为评论设置cookie。它允许你设置cookies,当用户提交评论时,可以用来记住他们的名字、电子邮件地址和网站。

设置用于存储未经认证的评论员身份的cookies。通常用于调用该评论者以前的评论,这些评论仍被保留在审核中。

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
	 *
	 * @param int $seconds Comment cookie lifetime. Default 30000000.
	 */
	$comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 );

	$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 );
}

常见问题

FAQs
查看更多 >