0

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:

  1. 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.
  2. Expiration should be 1 year.
  3. Cookie should work across my domain including subdomains. At the moment my domain is development.mydomain.com
  4. 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.

  • 1
    you sure it is a coy&paste of your code? the function name for the 'init' action is different the name of the function for which you show the code.... – Mark Kaplun Oct 28 '13 at 5:52
  • my apologies. I corrected it now. It is an exact copy of my code minus the actual function name. – Damainman Oct 28 '13 at 16:13
  • 1
    Are you using any form of "caching" plugin? Super Cache? Total Cache? – Otto Oct 28 '13 at 18:31
  • No but I am hosted on wp-engine. I updated my post, because it looks like another plugin I use is now also having the same issue where it only fully works when logged in as admin. – Damainman Oct 31 '13 at 16:05
  • 1
    have you seen this? – Milo Oct 31 '13 at 16:08
1

My Host wpengine.com was caching pages which caused the issue. I had to request that they uncache specific pages for everything to work normal again.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.