At first I thought it was a browser issue, but it looks like my code only sets a cookie if the user is logged in the administrator. If you are not logged into the Administrator account then it does not set a cookie. Tested this across 3 different browsers using Windows 8.
Just for reference, here is the link to the question when I thought it was a cross browser issue:
My code is inside an activated custom plugin I created, inside of a file called custom_functions.php (The only file in my plugin). This is my first plugin, but so far other functions I have placed in there appear to work correctly.
Here is the code I am using:
add_action( 'init', 'visitor_cookie', 1);
function visitor_cookie() {
if (!isset($_COOKIE['returning_visit'])) {
$traffic_type = 'hd';
$timestring = microtime();
$pieces = explode(" ", substr($timestring, 2));
$pieces[0] = "1".substr($pieces[0],0,3);
$visitor_id = $traffic_type.dechex($pieces[1]) . dechex($pieces[0]);
$expire = time()+60*60*24*360;
$path = '/';
$domain = parse_url(get_option('siteurl'), PHP_URL_HOST);
setcookie(
'returning_visit',
$visitor_id,
$expire,
$path,
$domain,
false
);
}
}
Notes:
- I only want the cookie set for any visitor/user is not an administrator. So only set for random users and regular users. But not necessary if too difficult.
- Expiration should be 1 year.
- Cookie should work across my domain including subdomains. At the moment my domain is development.mydomain.com
- In add_action , I am not sure if the "1" should be there.
In the link for my other question, I was informed that "parse_url(get_option('siteurl'), PHP_URL_HOST);" is depreciated and I should use site_url(). However when I used " $domain = site_url() ", cookies did not set at all so I am not sure what exactly I should use to set the domain variable properly.
Although the above code works only when logged in as the administrator, I am new to both plugins and setting cookies and not sure if my code is up to date or the most efficient/correct way to go about it.
Update:
So I just noticed there are other website functions only working as Admin.. such as grabbing GET parameters from the formidable pro plugin.
I tried disabled almost all my plugins and still the same issue. Something is causing some things to work only when logged in as admin and not sure what.