With enqueue you would change $deps
to your script or array of scripts that are needed to run first for your script to work.
<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>
For example:
function script_that_requires_jquery() {
wp_register_script( 'script-with-dependency', 'http://www.example.com/script-with-dependency.js', array( 'jquery' ), '1.0.0', true );
wp_enqueue_script( 'script-with-dependency' );
}
add_action( 'wp_enqueue_scripts', 'script_that_requires_jquery' );
I usually do this but it doesn't always seem to work:
function script_that_requires_jquery(){
if ( wp_script_is( 'jquery', 'done' ) ) { ?>
<script type="text/javascript">
(function($) {
// jquery function goes here
})( jQuery );
</script>
<?php }
}
add_action('wp_footer', 'script_that_requires_jquery');
Maybe this is it?
function script_that_requires_jquery(){
wp_enqueue_script( 'jquery' );
?>
<script type="text/javascript">
(function($) {
// jquery function goes here
})( jQuery );
</script>
<?php
}
add_action('wp_footer', 'script_that_requires_jquery');