Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress force scripts to load after plugins

I have a custom js file as part of my wordpress installation, which is registered and enqueued using functions.php as normal. I have registered this particular script to load within wp_footer rather than within wp_header. However, I also have a plugin that is putting js into wp_footer, and by default it is loading this below my script. I need to force my script to load last. Is there soemthing simple that can be done to achieve this? It seems like something fairly routine to me?

Here is my code:

function scripts() {
// Register scripts
// js
wp_register_script('custom', get_bloginfo('template_url'). '/js/custom.js', false, false, true);

// Enqueue scripts
wp_enqueue_script('custom');
}

add_action('wp_enqueue_scripts', 'scripts');

The current order of the scripts goes, but I need to force my script to load after the plugin:

<script>/my_script.js</script>
<script>/plugin_script.js</script>

Thanks in advance!


1 Answers

Just set the $priority parameter to a ridiculously high value:

add_action( 'wp_enqueue_scripts', 'scripts', 999999 );

To be the very last in the queue (though one can never be sure) use php's PHP_INT_MAX constant:

add_action( 'wp_enqueue_scripts', 'scripts', PHP_INT_MAX );

Edit cf. discussion in comments. Note that the following is NOT the recommended way of enqueueing scripts in the footer.

add_action( 'wp_print_footer_scripts', 'so26898556_print_footer_scripts', ( PHP_INT_MAX - 2 ) );
function so26898556_print_footer_scripts()
{
    echo '<script src="' . get_bloginfo( 'template_url' ) . '/js/custom.js"></script>';
}
like image 105
diggy Avatar answered May 02 '26 16:05

diggy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!