Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to use wp_enqueue_script() for inline scripts?

I'm doing this because my inline script depends on another script and i would like the flexibility of inserting it after it's loaded.

Also, It's an inline script because i'm passing php variables into the javascript (like theme path, etc)

Thanks in advance.

share|improve this question

3 Answers

up vote 8 down vote accepted

Well, you have wp_localize_script(), but that's only for passing data.

Otherwise, you can do this:

function print_my_inline_script() {
  if ( wp_script_is( 'some-script-handle', 'done' ) ) {
?>
<script type="text/javascript">
// js code goes here
</script>
<?php
  }
}
add_action( 'wp_footer', 'print_my_inline_script' );

The idea is that you shouldn't rely on your inline script being printed exactly after the script it depends on, but later.

share|improve this answer

Just don't make it an inline script and then pass the dynamic parameters to it as variables. Otto has written a great guide on how to do this effectively.

WordPress 3.3 will also make this more powerful: https://core.trac.wordpress.org/ticket/11520

share|improve this answer

I believe you could put your inline scripts into a inline-scripts.js file within your theme, then call that via wp_enqueue_script. Would that work for your needs? Best of luck!

share|improve this answer
1  
Well then it wouldn't be an inline script. – chrisjlee Aug 3 '11 at 22:55

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.