jQuery TipsProgramming TipsWordPress Tips

WP_Enqueue_Script Inline Script To Load After jQuery

The WordPress Codex doesn’t seem to have an answer for how to enqueue an inline script so that it can load with all the right dependencies — principally jQuery in most cases. Luckily, with some quick thinking, we can hook into the enqueue action and achieve this ourselves fairly easily.

Load Inline Script After jQuery WP Enqueue Script in WordPress

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

All we are doing is surrounding the inline script with a PHP function that hooks into the wp_footer action to fire only after jQuery has been loaded, along with the DOM.

Join the discussion 7 Comments

Leave a Reply

Allen Gingrich

Author Allen Gingrich

Allen has worked on the web for over a dozen years. Like many young entrepreneurs, he began with a small workspace in his basement, where he eventually formed Ideas and Pixels. The rest, well, is history. Allen enjoys fine wines, weight training, and beautiful, semantic code.

More posts by Allen Gingrich

Join the discussion 7 Comments

Leave a Reply