I'm making a theme where I'm including a javascript-lib which wants to be loaded/instrumented with a potion of generated code at document.ready()-time. Generated because some theme-options are influencing the behaviour.

I properly load the .js of the lib with wp_enqueue_script(). Now I need a proper way to put the the javascript-code below the script.

I found a way which suggests to change header.php to include the code after the call to wp_head().

I think my question is, is there a way to add js-code so that the call to wp_head() includes at in the right place? Is there a wp_enqueue_script_code()?

1 Answer 1


To print JavaScript code to your page, you have several paths to choose from. The most obvious would be the wp_footer action. Just hook a function to it and make it print your JavaScript/jQuery code, wrapped in <script /> tags.

As an addition, if your custom JavaScript/jquery code is static (i.e., there are no dynamic parts in it), you should put it in a separate JS file and enqueue it with the lib as dependency. But you explicitly asked for printing so that's what I answered.

As for your comment, as long as your code doesn't have to be executed before the page has been rendered—which doesn't seem to be the case, according to your question (document.ready)—this is just best practice to enqueue script files in the footer.

  • Thanks for your fast answer. So, the same way I could hook an action to wp_head(), couldn't I? If not, why is wp_footer() better suitable?
    – Patrick B.
    Dec 21, 2014 at 21:13
  • 1
    Yes you can do the same with wp_head action. But footer is always recommended for scripts unless you need your scripts in header for some specific purpose.
    – Robert hue
    Dec 22, 2014 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.