wc_setcookie

Set a cookie - wrapper for setcookie using WP constants.

Description

wc_setcookie( (string) $name, (string) $value, (int) $expire = 0, (bool) $secure = false ); 

Parameters (4)

0. $name (string)
Name of the cookie being set.
1. $value (string)
Value of the cookie.
2. $expire — Optional. (int)
Expiry of the cookie.
3. $secure — Optional. (bool) => false
Whether the cookie should be served only over https.

Usage

  1. if ( !function_exists( 'wc_setcookie' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php'; 
  3.  
  4. // Name of the cookie being set. 
  5. $name = ''; 
  6.  
  7. // Value of the cookie. 
  8. $value = ''; 
  9.  
  10. // Expiry of the cookie. 
  11. $expire = -1; 
  12.  
  13. // Whether the cookie should be served only over https. 
  14. $secure = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wc_setcookie($name, $value, $expire, $secure); 
  18.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-core-functions.php  
  1. function wc_setcookie( $name, $value, $expire = 0, $secure = false ) { 
  2. if ( ! headers_sent() ) { 
  3. setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure ); 
  4. } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  5. headers_sent( $file, $line ); 
  6. trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE );