wp_set_comment_cookies() WordPress Function
This function sets the cookies on the user's computer for the comment system on a Wordpress site. This is necessary to allow the user to login to the site and leave comments. The function sets three cookies: the comment author's name, the comment author's email address, and a URL.
wp_set_comment_cookies( WP_Comment $comment, WP_User $user, bool $cookies_consent = true ) #
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
- $comment
(WP_Comment)(Required)Comment object.
- $user
(WP_User)(Required)Comment author's user object. The user may not exist.
- $cookies_consent
(bool)(Optional) Comment author's consent to store cookies.
Default value: true
Source
File: wp-includes/comment.php
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 );
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 4.9.6 | The $cookies_consent parameter was added. |
| 3.4.0 | Introduced. |