I was wondering if someone could help me out. I am currently trying to set up the google maps Javascript API to work with wordpress on a localhost but i have had no luck. Please find my steps below.
-Step 1: I went to the Google Developer API Manager and enable an api for Google Maps Javascript API.
-Step 2: I generated an API Key.
-Step 3: I enqued the maps Javascript API like so and included the API key where it says key={MY API KEY} :
if ( ! function_exists( 'foundationpress_scripts' ) ) :
function foundationpress_scripts() {
//Load Google Maps API
wp_enqueue_script('google-maps', 'https://maps.googleapis.com/maps/api/js?key={MY API KEY}' );
//Load custom JS script
wp_enqueue_script('custom-scripts', get_stylesheet_directory_uri() . '/assets/javascript/custom/custom-scripts.js', array(), '1.0.0', true );
// Add the comment-reply library on pages where it is necessary
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'foundationpress_scripts' );
endif;
-Step 4: I created a custom-scripts.js file and loaded to the directory i enqued it to.
-Step 5: I added
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
to the custom-scripts.js file.
Step 6: I created a template page and linked it to a page inside the dashboard and added <div id="map-canvas"></div>
.
I am not receiving any WP_DEBUG errors nor am I receiving any developers console errors. Does anyone know why this is happening. I appreciate the help.
custom-scripts
JS should specify yourgoogle-maps
script as a dependency though.foundationpress_scripts()
exists in the parent theme, your code will not run. Remove thefunction_exists()
conditional check around your function. Note that this will override the parent theme'sfoundationpress_scripts()
function. If you don't want that to happen, rename yourfoundationpress_scripts()
to something else.