0

I want to write a plugin that enables a function to be put in my theme in various places. Then I would like to collect the parameters called and generate a Javascript and put it into my site <head>.

For instance, my theme would be:

<?php
get header();
?>
<div>
     <?php myFuncion("foo"); ?>
</div>
<manyotherdivs>
       ...
</manyotherdivs>
<aside>
     <?php myFunction("bar"); ?>
</aside>
 .... and so on

Then I would like to generate this JS:

<script>
var params = ["foo", "bar", "baz", ...];
</script>

I can collect the parameters by using a class and a static property, but it seems that they will be ignored in the wp_header hook, returning an empty array.

5
  • 2
    The only solution is output buffering, which I always like to avoid - is there no possibility of adding the JS to the end of the document? Can you be a bit more specific about your goal/why you need the JS in the head - this will help avoid an XY problem. Oct 12 '15 at 14:56
  • @TheDeadMedic sadly, it's a requirement I have to fulfill for a marketing team.
    – Cranio
    Oct 12 '15 at 15:01
  • @TheDeadMedic and I hate manipulating the buffer too. (Plus, +1 for the XY problem, finally I have found a suitable name for my usual way of answering many online help requests :D)
    – Cranio
    Oct 12 '15 at 15:03
  • 1
    Well, buffer if it is then ;) Are you happy to carry on coding or do you want some pointers? Oct 12 '15 at 15:05
  • @TheDeadMedic Nope, I can do by myself. Thank you anyway for offering your help :)
    – Cranio
    Oct 12 '15 at 15:07

Your Answer

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

Browse other questions tagged or ask your own question.