jlittlejohn
9/25/2013 - 1:25 AM

WP: Setting & Getting Cookies

WP: Setting & Getting Cookies

// http://php.net/manual/en/function.setcookie.php

// functions.php
function set_newuser_cookie() {
  if (!isset($_COOKIE['sitename_newvisitor'])) {
    setcookie('sitename_newvisitor', 1, time()+1209600, '/', 'example.com', false);
  }
}

add_action( 'init', 'set_newuser_cookie');

// Theme
if (isset($_COOKIE['sitename_newvisitor'])) {
  echo 'Welcome back!';
}
else {
  echo 'Hello new visitor!';
}