0

I'm trying to create a page which sets a cookie so I can exclude people from Google Analytics in a WordPress site

I'm trying to set this up using custom jQuery, but there is something wrong with the code I'm using

Here's the link code

<a id="hidemelink" href="#">Click to Set Cookie</a>`

and the jQuery code

jQuery('#hidemelink').click(function(){
    jQuery.cookie("HideMe", "HideMe", {
        expires : 400, 
        path    : '/'  
    };
    alert('HideMe cookie set');
    return false;
});

The problem is with the jQuery.cookie bit. If I remove

jQuery.cookie("HideMe", "HideMe", {
    expires : 400, 
    path    : '/'  
};

and just leave the alert & return statements, the code executes fine - the alert is displayed and I stay on the page.

However, when I put the cookie code in, the alery isn't displayed & I'm redirected to the home page.

This is probably something incredibly simple, but I just can't see it. Help would be much appreciated

Many thanks in advance

Dekel
  • 60,707
  • 10
  • 101
  • 129
Trevor Wood
  • 251
  • 1
  • 3
  • 4
  • Are you sure you loaded the jquery-cookie correctly? Did you get any errors in the console? – Dekel Sep 14 '17 at 14:58
  • Sorry, I don't understand the question. WordPress has jQuery build in and should be usable straight away. I'm not a jQuery erson - everything I've learned is from Google this afternoon and there's nothing about loading the cookie. The cookie doesn't exist, this is why I'm trying to create & set it – Trevor Wood Sep 14 '17 at 15:00
  • jquery is not jquery-cookie. Check here: https://github.com/carhartl/jquery-cookie (note that this lib is **deprecated**) - Regarding the loading - I was asked if you loaded the **library** jquery-cookie. Not the cookie itself :) – Dekel Sep 14 '17 at 15:05
  • I'm not trying to use jquery-cookie. One of the original pages I looked at was https://stackoverflow.com/questions/1458724/how-do-i-set-unset-cookie-with-jquery which tells me how to set, get and delete the cookie. This is WordPress we're talking about and loading other libraries can mean tampering with the theme – Trevor Wood Sep 14 '17 at 15:13
  • You can't say "I'm not trying to use jquery-cookie" when you have `jQuery.cookie` in your code. This is using jquery-cookie. – Dekel Sep 14 '17 at 15:22
  • If you really check the link you just added in the comment you will see there the same link to the jquery-cookie lib I added as well. – Dekel Sep 14 '17 at 15:23
  • Ok - I thought this was part of the jQuery package. How do I get jQuery.cookie into WordPress? – Trevor Wood Sep 14 '17 at 15:23
  • You read the manual :) – Dekel Sep 14 '17 at 15:24

0 Answers0