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.