wp enqueue inline script due to dependancies

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.

6

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.

Leave a Comment