• Resolved Jan444

    (@ign2009)


    Hi, thanks for this great plugin.

    I have a cookie in the footer.php file that I would like to get covered by the plugin.

    Usually we have to remove it from the template, and place it on the script box on the settings area.

    However, this cookie is a conditional one: if we are in page A the cookies is “cookie A”, and if we are in page B is “cookie B”.

    The box would be applied the same everywhere – so I wonder how can I workaround this situation?

    Thanks,

Viewing 9 replies - 1 through 9 (of 9 total)
  • Ambyomoron

    (@josiah-s-carberry)

    If the code in footer.php, which is applied everywhere, includes all the logic you describe above, what is to prevent you from using the same logic in the code in the script box, which is applied everywhere (or nowhere)?

    Thread Starter Jan444

    (@ign2009)

    Hi Josiah, so can we add conditional PHP tags inside the Script blocking box?

    My goal is sending a cookie conditionally, like so:

    If user is in Page “Thank You for your Inquiry”, track him with this cookie, but if he’s elsewhere, do not

    I thought that if I simply put the cookie on the Script blocking box, it will apply everywhere, instead of a particular page – because it loads on wp_footer() regardless of the page.

    Cheers,

    Ambyomoron

    (@josiah-s-carberry)

    I don’t really know. Just try it. If it doesn’t work, then I would raise a bug report with the plugin author.

    In any case, if the plugin runs the script, then it will run everywhere, so you need to include the logic of checking what page you’re on within the script.

    Thread Starter Jan444

    (@ign2009)

    Hi Josiah, I tried it but it doesn’t accept PHP conditional tags on the Script Blocking area (to show accepted cookies based on the page the user is).

    Maybe the plugin’s author can suggest an alternative?

    Many thanks,

    Ambyomoron

    (@josiah-s-carberry)

    I note the following remarks at https://codex.www.ads-software.com/Conditional_Tags :

    Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

    However: if you have a reference to the query object (for example, from within the parse_query or pre_get_posts hooks), you can use the WP_Query conditional methods (eg: $query->is_search())

    Can’t you can implement the logic you need in a different way?

    Thread Starter Jan444

    (@ign2009)

    Hi Josiah, I had no luck with that, as this seems to happen in the template level, instead of the Blocking box.

    However I have found another instance when out-of-the-Box conditionality would be needed (“if Cookies Accepted, send cookie – otherwise don’t”), and that is when we have to set cookies in some specific area on the template, for example after the content.

    It can occur if we use Google Ads for example.

    Google tells me to install this code (that will send all kind of advertisement cookies) in the desired place on our templates:

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
         style="display:block; text-align:center;"
         data-ad-layout="in-article"
         data-ad-format="fluid"
         data-ad-client="ca-pub-5945788092231686"
         data-ad-slot="6020271303"></ins>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({});
    </script>

    How can we do so the plugin affects this kind of cookies?

    How to show up this code only when people have previously Accepted the site’s cookies via the plugin?

    The authors are invited to drop a line around here too! ??

    Cheers,

    • This reply was modified 6 years, 8 months ago by Jan444.
    Ambyomoron

    (@josiah-s-carberry)

    How to show up this code only when people have previously Accepted the site’s cookies via the plugin?

    This is precisely what the plugin does, if (and only if) you put your script in he script blocking field.

    …when we have to set cookies in some specific area on the template, for example after the content.

    If you are talking about where the ad appears on the page, then this has nothing to do with the cookie plugin. If you are talking about if the ad appears or not, then this is what the plugin does. It runs the script you showed above only if the visitor has accepted the cookies. And if you are talking about when the script gets run (if cookies are accepted), then the plugin allows you to run it before the </head> tag in the page or somewhere between the <body> and </body> tags.

    Thread Starter Jan444

    (@ign2009)

    Hi Josiah, thanks for your help.

    I think I understand the plugin’s logic, however it only applies when we use the Blocking Box.

    If for whatever reason(*) we need to hardcode a cookie on our templates, we are out of luck, unless we resort to some workaround.

    I am trying with this on my templates (header.php / single.php) and I think it may work:

    <?php 
    if(isset($_COOKIE['cookie_notice_accepted']) && $_COOKIE['cookie_notice_accepted']== "true" ) {
         echo '<script href="YOUR SCRIPT WITH COOKIES HERE"></script>
    	<script>
    	  //YOUR SCRIPT WITH COOKIES HERE
    	</script>';
         }
         else {
          echo '<!-- cookies not accepted yet -->';
         }
    	 ?>

    Unless I’m missing something!

    Regards,

    (*): conditionality on a multi-language site, or conditionality depending on the page loaded, or because of advertisments placement reasons, etc.

    • This reply was modified 6 years, 8 months ago by Jan444.
    Thread Starter Jan444

    (@ign2009)

    I can confirm my code avobe does work.
    Thanks very much for your help guys.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Conditional scripts – how to apply them?’ is closed to new replies.