Wp_head and wp_footer vs wp_enqueue_script javascript files?

I’m converting a bootstrap html website into WordPress.

Now, all recommend using wp_enqueue_script() for registering javascript files. i.e.

function themeslug_enqueue_script() {
    wp_enqueue_script( 'my-js', 'filename.js', false );
}

add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );

But I have some js files loading in the head and some other ones in the footer of the original html code.

I was going to use wp_head and wp_footer, is it ok to do so or better stick to wp_enqueue_script?

1 Answer
1

It looks like you’ve overlooked the 5th input parameter of:

 wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); 

Namely the boolean $in_footer part, that will put the script in the footer, if set as true, else in the header.

You can also use $deps to handle dependencies.

Check out the Codex for more info.

=> I would stick to wp_enqueue_script() and not manually place the scripts into the header or footer.

Leave a Comment