Search code examples
phpjavascriptjoomlacontent-management-systemvirtuemart

Need help hiding some code


I am working on a Joomla(1.5.14)/Virtuemart(1.1.3) website and I have run into a problem implementing some AdWords code snippets due to being unable to use PHP in the Category Description areas in Virtuemart.

This is what I wanted to achieve.

I have the main AdWords snippet in the main 'index.php' file for the website as follows:

<?php if ($HideAdWords != "yes") : ?>

<!-- Google Code for Every user Remarketing List -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = ###;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "666666";
var google_conversion_label = "###";
var google_conversion_value = 0;
/* ]]> */
</script>
<?php if ($_SERVER['HTTPS']) { ?>
<script type="text/javascript" src="https://www.googleadservices.com/pagead/conversion.js">
<?php } else { ?>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
<?php }?>

</script>
<noscript>
<div style="display:inline;">
<!--<img height="1" width="1" style="border-style:none;" alt="" src="###"/>-->
</div>
</noscript>

<?php endif; ?>

On certain pages I included a different AdWords code along with the following PHP snippet, wanting to remove the original piece of code from the page when browsing this page:

<?php
$HideAdWords = "yes";
?>

This did do what I wanted it to however on most of the pages I need to apply this I am not able to use PHP, when entering PHP into a category description in Virtuemart, upon saving changes it comments out any PHP code and makes it useless.

I have tried searching as much as I can, and tried the Joomla/Virtuemart specific websites/forums. I managed to get JavaScript working in these category description areas so I was wondering if maybe I can replicate this effect with JavaScript or maybe you guys can think of a better solution?

I hope I have been clear, any help would be really appreciated.

Kind Regards.

*edit: fixed unspecific title.


Solution

  • If you want to do this in JavaScript, use this snippet in your main page:

    <script>
    (function() {
      if(window.hideAdWords) return;
      var google_conversion_id = "###",
          google_conversion_language = "en",
          google_conversion_format = "3",
          google_conversion_color = "666666",
          google_conversion_label = "###",
          google_conversion_value = 0;
    
      document.write(unescape("%3Cscript%20type%3D%22text/javascript%22%20src%3D%22//www.googleadservices.com/pagead/conversion.js%22%3E%3C/script%3E"));
    }());
    </script>
    

    Then your PHP just needs to write out:

    <script>
      hideAdWords = <?php print ($HideAdWords == "yes" ? 'true':'false'); ?>;
    </script>
    

    If you aren't able to guarantee that the hideAdWords line would be printed before the function block, you can change the function block so that it is wrapped in an event handler that fires when the page has finished loading (this example uses jQuery but you can easily do the same thing with an addEventListener call):

    <script>
    jQuery(function() {
      if(window.hideAdWords) return;
      var google_conversion_id = "###",
          google_conversion_language = "en",
          google_conversion_format = "3",
          google_conversion_color = "666666",
          google_conversion_label = "###",
          google_conversion_value = 0;
    
      document.write(unescape("%3Cscript%20type%3D%22text/javascript%22%20src%3D%22//www.googleadservices.com/pagead/conversion.js%22%3E%3C/script%3E"));
    });
    </script>
    

    Given that the only difference between the SSL and HTTP urls is the schema, you can drop the protocol and start the url with a double slash to just use the http or https that the current page has been accessed through.