Title: wp_set_comment_cookies
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_set_comment_cookies( WP_Comment $comment, WP_User $user, bool $cookies_consent = true )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#changelog)

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](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#parameters)󠁿

 `$comment`[WP_Comment](https://developer.wordpress.org/reference/classes/wp_comment/)
required

Comment object.

`$user`[WP_User](https://developer.wordpress.org/reference/classes/wp_user/)required

Comment author’s user object. The user may not exist.

`$cookies_consent`booloptional

Comment author’s consent to store cookies.

Default:`true`

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#source)󠁿

    ```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
    	 * @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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/comment.php#L639)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/comment.php#L639-L670)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#hooks)󠁿

 [apply_filters( ‘comment_cookie_lifetime’, int $seconds )](https://developer.wordpress.org/reference/hooks/comment_cookie_lifetime/)

Filters the lifetime of the comment cookie in seconds.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#related)󠁿

| Uses | Description | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [home_url()](https://developer.wordpress.org/reference/functions/home_url/)`wp-includes/link-template.php` |

Retrieves the URL for the current site where the front end is accessible.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_set_comment_cookies/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.9.6](https://developer.wordpress.org/reference/since/4.9.6/) | The `$cookies_consent` parameter was added. | 
| [3.4.0](https://developer.wordpress.org/reference/since/3.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_comment_cookies%2F)
before being able to contribute a note or feedback.