Title: Cookie block
Last modified: May 6, 2018

---

# Cookie block

 *  Resolved [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/)
 * (@geoffmartyn)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/)
 * Hi – this is a great plugin and is by far the best one out there for cookies 
   and general GDPR compliance.
 * I’m just tearing my hair out in terms of how to block the GA script by default.
 * I’d like to include this via a helper function in functions.php but I’ve added
   this in and I just get a 500 error. As far as I can see the syntax is correct,
   it just dies when I add this in.
 *     ```
       function analytics_snippet() {
         if ( is_allowed_cookie( '_ga' ) {
       ?>
   
            <script>
         (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
         (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
         m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
         })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
   
         ga('create', 'UA-xxxxxxxx-x', 'auto');
         ga('send', 'pageview');
   
       </script>
       <?php
         }
       }
       ```
   
 * (xxxxxx-x is replaced with my analytics ID)
 * I’ve set-up a tab called ‘analytics’ and listed the cookie I’m trying to block
   as _ga
 * Am I missing something really simple or should this work?
 * Thank you!
 * Geoff
    -  This topic was modified 7 years, 11 months ago by [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/).
      Reason: Forgot to mention that I replace the XXXX-X placeholder
    -  This topic was modified 7 years, 11 months ago by [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/).

Viewing 14 replies - 1 through 14 (of 14 total)

 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10254731)
 * Hi [@geoffmartyn](https://wordpress.org/support/users/geoffmartyn/)
 * What hook are you using with this?
 *  Thread Starter [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/)
 * (@geoffmartyn)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10255202)
 * Hi Fernando,
 * Sorry, I’ve tried to add it via wp_head, but overall I’m just trying to block
   a single cookie so I can see how it works and then I can go from there.
 *     ```
       function analytics_snippet() {
         if ( is_allowed_cookie( '_ga' ) {
       ?>
   
            <script>
         (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
         (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
         m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
         })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
   
         ga('create', 'UA-xxxxxxxx-x', 'auto');
         ga('send', 'pageview');
   
       </script>
       <?php
         }
       }
   
       add_action('wp_head','analytics_snippet');
       ```
   
    -  This reply was modified 7 years, 11 months ago by [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/).
    -  This reply was modified 7 years, 11 months ago by [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/).
 *  Plugin Author [Anthony Moore](https://wordpress.org/support/users/amooreto/)
 * (@amooreto)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10256198)
 * Hi [@geoffmartyn](https://wordpress.org/support/users/geoffmartyn/),
 * One of your IF statements is missing a bracket.
 * It should be:
 *     ```
       if ( is_allowed_cookie( '_ga' ) ) {
       ```
   
 *  Thread Starter [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/)
 * (@geoffmartyn)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10256262)
 * Hi [@amooreto](https://wordpress.org/support/users/amooreto/),
 * You are a star! Thank you – I knew it was something really simple!
 * That’s in now, but I still can’t get it to delete the cookie when I change the
   preference in the cookie popup.
 * In the examples here:
 * “…assuming you registered an analytics consent and _ga as one of the cookies 
   used…”, the line from the my_google_opt_out function:
 * `if ( ! has_consent( 'analytics' ) || ! is_allowed_cookie( '_ga' ) ) {`
 * …doesn’t seem to do anything.
 * The page also mentions that it assumes I have ‘registered an analytics consent’
   but does this refer to a tab in the cookies settings page or a category name 
   of cookie?
 * Is there an example online anywhere of an implementation of this plugin that 
   will actually remove and then re-set Google Analytics cookies (for example) once
   it’s approved again? I’d just love to see if it’s possible, or if I’m barking
   up the wrong tree.
 * Thank you once again!
 * Geoff
    -  This reply was modified 7 years, 11 months ago by [geoffmartyn](https://wordpress.org/support/users/geoffmartyn/).
 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10258746)
 * Hi [@geoffmartyn](https://wordpress.org/support/users/geoffmartyn/),
 * The plugin will prevent the cookie from being created but it will not delete 
   the cookie. Not in the current version.
 * I’m preparing a release with this feature. But rest assured that even if the 
   _ga cookie is still there, if you are not accepting the _ga cookie or the consent
   it will not send analytical data to Google.
 *  [prototypead](https://wordpress.org/support/users/prototypead/)
 * (@prototypead)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10263744)
 * Hi, I am trying to understand how blocking/enabling cookies works. This thread
   has been helpful, I just want to put it in plain language what the steps are.
   Please let me know if they are correct.
 * 1. Identify Cookies used on the website
    2. Register cookies in the plugin under
   Cookies Tab 2a. Add Category name ‘analytics’ 2b. identify cookies used – ‘_ga,
   _gid, _gat’ 3. Add the code below in the functions.php file
 *     ```
       add_action( 'wp_head', 'my_google_opt_out' );
       function my_google_opt_out() {
         if ( ! has_consent( 'analytics' ) || ! is_allowed_cookie( '_ga' ) ) {
           ?>
           <script>
         (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
         (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
         m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
         })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
   
         ga('create', 'UA-xxxxxxxxx-x', 'auto');
         ga('send', 'pageview');
   
       </script>
           <?php
         }
       }
       ```
   
 * But what does this do. If the cookies are off, then this function will not make
   them available and then google will not track? Do we need to create analytics
   consent too?
 * What about _gid and _gat? Does a separate helper function for each need to be
   added?
 * What if we have Facebook Pixel on the site? Is a new category need to be created
   and a new helper function in function.php file has to be added?
 * Thank you for your help, I know it is basic questions, but I just want to understand
   how to properly implement it.
 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10263801)
 * I can’t legally tell you what types of consent you need. There are infinite cases
   out there. You can have privacy policy only, you can have analytics consent, 
   you can have none, it’s up to you to decide how to structure your stuff.
 * GA sets, in general, 3 cookies. You only need to check for one of them. In this
   case, _ga.
 * This will turn GA off entirely. You can do that or you can google how to make
   your google analytics data anonymous. That way there’s no PII involved and should
   be OK. Again, I’m not a lawyer. You should talk to one.
 * For FB pixel it all depends on how you are adding it. If it is the same way, 
   the same thing would apply, but with a different cookie name or consent type.
 *  [prototypead](https://wordpress.org/support/users/prototypead/)
 * (@prototypead)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10263857)
 * Thank you, I understand legally you cannot advise, we are talking to some regarding
   that. I just wanted to make sure the code to use and how to use it is the right
   way.
 * So if we do not require a separate consent for google analytics, I would use 
   the code like this in the if section?
 *     ```
       function my_google_opt_out() {
         if ( is_allowed_cookie( '_ga' ) ) {
       ....the rest code under here as above
       ```
   
 * Can I use something like this:
 *     ```
       function my_google_opt_out() {
         if ( is_allowed_cookie( '_ga', '_gid' ) ) {
       ....the rest code under here as above
       ```
   
 * Thank you. I will google how to make google analytics data anonymous too. Good
   suggestion.
 * ALSO, currently we have the google tracking code in header.php file. Do I remove
   it from there when adding helper function in functions.php file?
 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10263897)
 * You can’t use more than one cookie name with that function.
 * Yes. Remove the code from the header.php file.
 *  [diegoagreda](https://wordpress.org/support/users/diegoagreda/)
 * (@diegoagreda)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10263928)
 * This might be a dumb question [@fclaussen](https://wordpress.org/support/users/fclaussen/).
   So we register a cookie then we have to add the lines of code? Thanks
 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10263937)
 * Yep. Sadly there’s no way for me to block cookies for users. So yea, extra code
   is needed. 🙂
 *  [prototypead](https://wordpress.org/support/users/prototypead/)
 * (@prototypead)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10264020)
 * Thank you, so if we need to remove Facebook Pixel cookie, I would register it
   in the plugin and then add a helper function next to google analytics function
   with the new name and whatever FB pixel cookie is named. Something like that:
 *     ```
       function my_facebook_opt_out() {
         if ( is_allowed_cookie( '_fbq' ) ) {
       ....the rest code under here 
       ```
   
 *  Plugin Author [Fernando Claussen](https://wordpress.org/support/users/fclaussen/)
 * (@fclaussen)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10264076)
 * Yep
 *  [prototypead](https://wordpress.org/support/users/prototypead/)
 * (@prototypead)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10264097)
 * Thank you for answering all my silly question [@fclaussen](https://wordpress.org/support/users/fclaussen/).
   It makes more sense now.

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Cookie block’ is closed to new replies.

 * ![](https://ps.w.org/gdpr/assets/icon.svg?rev=1837142)
 * [GDPR](https://wordpress.org/plugins/gdpr/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/gdpr/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/gdpr/)
 * [Active Topics](https://wordpress.org/support/plugin/gdpr/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/gdpr/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/gdpr/reviews/)

 * 14 replies
 * 5 participants
 * Last reply from: [prototypead](https://wordpress.org/support/users/prototypead/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/cookie-block/#post-10264097)
 * Status: resolved