0

I've been having some issues since Google requires an API code (which I already have) to display the map on the theme page, but the thing is I'm not sure where and how it should be enqueued.

This is what it looks like on the code:

function sano_footer_scripts() {

//    wp_enqueue_script( 'jquery_1.11.2_min_js',         get_template_directory_uri() . '/js/jquery-1.11.2.min.js','','', true);
//    wp_enqueue_script( 'google_js',                    'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false','','', true);
//    wp_enqueue_script( 'jquery_autosize_js',           get_template_directory_uri() . '/js/jquery.autosize.js','','', true);
//    wp_enqueue_script( 'jquery_smooth_scroll_min_js',  get_template_directory_uri() . '/js/jquery.smooth-scroll.min.js','','', true);
//    wp_enqueue_script( 'selectivizr_min_js',           get_template_directory_uri() . '/js/selectivizr-min.js','','', true);
//    wp_enqueue_script( 'jquery_watermark_min_js',      get_template_directory_uri() . '/js/jquery.watermark.min.js','','', true);
//    wp_enqueue_script( 'read_more_js',                 get_template_directory_uri() . '/js/read_more.js','','', true);
//    wp_enqueue_script( 'jquery_mixitup_min_js',        get_template_directory_uri() . '/js/jquery.mixitup.min.js','','', true);
//    wp_enqueue_script( 'jquery_magnific_popup_min_js', get_template_directory_uri() . '/js/jquery.magnific-popup.min.js','','', true);
//    wp_enqueue_script( 'jquery_animateNumber_min_js',  get_template_directory_uri() . '/js/jquery.animateNumber.min.js','','', true);
//    wp_enqueue_script( 'select2_min_js',               get_template_directory_uri() . '/js/select2.min.js','','', true);
//    wp_enqueue_script( 'owl_carousel_min_js',          get_template_directory_uri() . '/js/owl.carousel.min.js','','', true);
//    wp_enqueue_script( 'parallax_plugin_min_js',       get_template_directory_uri() . '/js/parallax-plugin.min.js','','', true);
//    wp_enqueue_script( 'jquery_nicescroll_min_js',     get_template_directory_uri() . '/js/jquery.nicescroll.min.js','','', true);
//    wp_enqueue_script( 'jquery_inview_min_js',         get_template_directory_uri() . '/js/jquery.inview.min.js','','', true);
//    wp_enqueue_script( 'jquery_validate_js',           get_template_directory_uri() . '/js/jquery.validate.js','','', true);
//    wp_enqueue_script( 'jquery_hoverdir_js',           get_template_directory_uri() . '/js/jquery.hoverdir.js','','', true);
//wp_enqueue_script( 'map_default_js',               get_template_directory_uri() . '/js/map-default.js','','', true);
//wp_enqueue_script( 'responsiveslides_min_js',      get_template_directory_uri() . '/js/responsiveslides.min.js','','', true);
//wp_enqueue_script( 'jquery_ui_js',                 get_template_directory_uri() . '/js/jquery-ui.js','','', true);
//wp_enqueue_script( 'init_js',                      get_template_directory_uri() . '/js/init.js','','', true);
//wp_enqueue_script( 'respond_src_js',               get_template_directory_uri() . '/js/respond.src.js','','', true);
 }

//add_action( 'wp_enqueue_scripts', 'sano_footer_scripts' );

Can anyone tell me where my API code should be?

1
  • Your way of enqueuing a script file is correct. If Google has different instructions, you should be able to find them on their site.
    – cjbj
    Jun 8, 2017 at 7:31

2 Answers 2

2

Your API key is URL encoded as a GET variable like so:

wp_enqueue_script( 'google_js', 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key=yourKeyHere', '', '' );

I don't urlencode the handle. The function/class does that. So if you're having problems getting the script to load, don't urlencode the handle.

0

You are using wp_enqueue_scripts() in the proper way, however you are enqueuing everything in the footer, and re queuing jQuery (which is not a good practice, you could use jquery-migrate instead).

But in response to your concern about the place to put this code, it really doesn't matter where in the functions.php you put this code. The scripts will be queued anyway. Although it is nicer to have such code in the first lines of functions.php for readability and ease of access.

There's nothing wrong with your code that i can see, you just need to unquote them by removing the // before every line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.