WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On the internet there are thousand of articles on how to insert external scripts using wp_enqueue_script().

Anyway i didn't find anything explanatory on how to add inline, <script> enclosed, scripts.

I use to add them in the middle of the page code but i suppose it's not the most elegant way and there should be a better way, so that the script code is automatically moved in the head or the footer.

Any ideas?

Edit: I forgot the ' around <script> so it got cancelled. Now my question should be clearer

share|improve this question
possible duplicate of wp enqueue inline script due to dependancies – Hareesh Sivasubramanian Sep 30 '15 at 11:00
ehm it's a quite old question you know? :) – Bakaburg Sep 30 '15 at 15:43
up vote 2 down vote accepted

Even though mrwweb is correct and that technique should be used, in reality nothing is perfect and there are times when inline scripts and styles are used. You can use wp script is to check for a script and output them in the header or footer using wp_head or wp_footer.

You can refer to scribu's answer in this post, wp enqueue inline script due to dependancies

share|improve this answer
So basically Scribu suggest to just print it in the footer via the wp_footer action? – Bakaburg May 9 '12 at 0:41
also the wp_localize_script is to be considered – Bakaburg May 9 '12 at 0:44
If needed, yes. – Wyck May 9 '12 at 0:45

wp_enqueue_script() is the only way that javascript should ever be added to your site. It allows you and other plugins to declare dependencies and even deregister scripts if needed. As mentioned on a different thread today, caching plugins can't gzip or minify your scripts if you don't use the proper WordPress technique.

If you look at the codex page, you'll see that you can control whether the script is in the header or footer.

share|improve this answer
i noticed that the <script> was cancelled by stackoverflow security system. Now my question should be more clear. I'm speaking about inline scripts that needs to know what's inside the page. – Bakaburg May 9 '12 at 0:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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