Search code examples
phpwordpressfooter

Moving external scripts to wordpress footer


I have some external scripts in my "wordpess head" like this:-

//blueimp.github.io/Gallery/js/jquery.blueimp-gallery.min.js
or https://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.0/masonry.pkgd.min.js

I want to move these scripts to my wordpress Footer. I know that these can be done via

function dequeue_my_scripts()  and function enqueue_scripts_to_footer() 

to use these functions i need a Script Handle. Has anybody an idea how i can move these external scripts?


Solution

  • If you load your scripts with wp_enqueue_script function, you can provide parameter (the last parameter in enqueue function), which tells WP to enqueue script directly to the footer of the site. Something like this:

    add_action( 'wp_enqueue_scripts', 'my_scripts' );
    function my_scripts() {
       //this script will be included in the WP foother because of the last `true` parameter
       wp_enqueue_script('masonry', 'https://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.0/masonry.pkgd.min.js', [], '3.3.0', true)
    }