The JS in the fiddle should be saved to a file, for example, scripts.js
. If this was in a folder in the theme root called js
you could enqueue the scripts like so:
function add_wp_scripts() {
wp_enqueue_script(
'google-maps',
'https://maps.googleapis.com/maps/api/js',
array(),
null,
null
);
wp_enqueue_script(
'script',
get_template_directory_uri() . '/js/script.js',
array(),
null,
true
);
}
add_action( 'wp_enqueue_scripts', 'add_wp_scripts' );
The final parameter sets whether the script is loaded in the <head>
or before the </body>
tag. Null will place the script in the <head>
, and obviously setting the parameter to true
will put it by the </body>
.
The script should be fine, as long as the codes good (!) and the Google Map script is loaded in the <head>
.
See also: https://developer.wordpress.org/reference/functions/wp_enqueue_script/