I'm trying to enqueue a JavaScript file in the header of my website.
If I add the following in my main plugin file the JS is included in the header:
function wpdocs_theme_name_scripts() { wp_register_script('googlesearch', 'https://maps.googleapis.com/maps/api/js'); wp_enqueue_script('googlesearch'); } add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
But I want to include the JS for a specific shortcode only, I add the following in the main plugin file:
function wpdocs_theme_name_scripts() { wp_register_script('googlesearch', 'https://maps.googleapis.com/maps/api/js'); } add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
And the following in the shortcode:
wp_enqueue_script('googlesearch');
Now the JS file is included in the footer of the website.
I've tried manually specifying that the JS should be loaded in the header like so but it still loads it in the footer:
wp_enqueue_script( 'googlesearch', 'https://maps.googleapis.com/maps/api/js', array(), '1.0.0', false );
Does anyone have any idea why this is happening? Appreciated any help!