Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 1 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
2  
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
add comment

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
add comment

Your Answer

 
discard

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.