Link to home
Start Free TrialLog in
Avatar of N Peter
N Peter

asked on

Load google maps api into wordpress

Hi,

I would like to load this directly into wordpress without using echo in the header.

This is the code I used originally. Function only runs on a certain page and it is working.

function loadComplexMap() {
	if (is_page(10)) {
		echo "<script src='http://maps.googleapis.com/maps/api/js?key=mykeycomeshere'></script>\n";
		echo "<script src='".get_template_directory_uri()."/lib/complexmap.js'></script>\n";	
	}
}

Open in new window


This is the one I would like to use instead but unfortunately map is not loading.

function loadComplexMap() {
	function complexaddmap() {
		wp_register_script( 'google-map-api', '//maps.googleapis.com/maps/api/js?key=mykeycomeshere', true);
		wp_register_script( 'complex-map', get_template_directory_uri() . '/lib/complexmap.js', true);
		wp_enqueue_script('google-map-api');
		wp_enqueue_script('complex-map');		
	}
	if (is_page(10)) {
		add_action('wp_enqueue_scripts', 'complexaddmap');		
	}
}

Open in new window

Any ideas would be useful

Thanks
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Usually it's wise to look for the experience of others in matters like this, where there are probably many others who have wanted to do the same sort of thing.  This plugin is very well respected:
https://wordpress.org/plugins/wp-google-maps/
Avatar of N Peter
N Peter

ASKER

Hi Ray,

Thank you for your suggestion but I would prefer to use my own code rather than a plug-in.
OK, then I'll sign off.  Good luck with your project.
ASKER CERTIFIED SOLUTION
Avatar of Edward Jimenez
Edward Jimenez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial