wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_set_comment_cookies › WordPress Function

Since3.4.0
Deprecatedn/a
wp_set_comment_cookies ( $comment, $user, $cookies_consent = true )
Parameters: (3)
  • (WP_Comment) $comment Comment object.
    Required: Yes
  • (WP_User) $user Comment author's user object. The user may not exist.
    Required: Yes
  • (bool) $cookies_consent Optional. Comment author's consent to store cookies. Default true.
    Required: No
    Default: true
Defined at:
Codex:
Change Log:
  • 4.9.6

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.



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