How to place script in footer?

jrcollins asked
javascript wp-enqueue-script footer
via

I have a script I want to place in my site’s footer. It’s not actually a file, just a single line of code (the script source is located at an external URL). So my question is, should I enqueue the script or just copy and paste it into the footer?


Answer
via

See wp_enqueue_script() and the $in_footer parameter it supports. Here’s an example of how you’d do this from the functions.php file for your theme. Note the last argument is true, which inserts the script into the footer.

<?php
add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_script( 'my-script', '/path/to/script.js', [], null, true );
} );
Share This
Posted in: