Add Google Maps to WordPress: Easy Code Snippet Guide
Explanation
To add Google Maps to your WordPress site without using a plugin, you can use a bit of custom code. Here's a simple way to do it:
Step 1: Enqueue Google Maps API
First, you need to load the Google Maps API script on your site. This is done by adding a function that includes your Google Maps API key. Make sure to replace 'YOUR_GOOGLE_MAPS_API_KEY' with your actual API key.
Step 2: Create a Shortcode
Next, you'll create a shortcode that allows you to easily embed a map anywhere on your site. This shortcode accepts attributes like latitude, longitude, zoom, width, and height. These attributes let you customize the map's location and appearance.
Using the Shortcode
Once the code is added to your theme's functions.php file, you can use the shortcode [google_map]
in your posts or pages. You can customize it by adding attributes like this:
[google_map latitude="40.7128" longitude="-74.0060" zoom="12" width="600px" height="450px"]
This will display a Google Map centered on the specified latitude and longitude, with the desired zoom level and dimensions.
Remember, this method requires you to have a valid Google Maps API key, which you can obtain from the Google Cloud Platform.
Code
<?php
// Function to enqueue Google Maps API script
function wp_dudecom_enqueue_google_maps_api() {
// Replace 'YOUR_GOOGLE_MAPS_API_KEY' with your actual Google Maps API key
$api_key = 'YOUR_GOOGLE_MAPS_API_KEY';
wp_enqueue_script('google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . esc_attr($api_key), array(), null, true);
}
add_action('wp_enqueue_scripts', 'wp_dudecom_enqueue_google_maps_api');
// Shortcode function to display Google Maps
function wp_dudecom_google_maps_shortcode($atts) {
// Extract shortcode attributes
$atts = shortcode_atts(
array(
'latitude' => '37.7749', // Default latitude
'longitude' => '-122.4194', // Default longitude
'zoom' => '10', // Default zoom level
'width' => '100%', // Default width
'height' => '400px' // Default height
),
$atts,
'google_map'
);
// Generate a unique ID for the map container
$map_id = 'wp-dudecom-map-' . uniqid();
// Output the map container and initialization script
ob_start();
?>
<div id="<?php echo esc_attr($map_id); ?>" style="width: <?php echo esc_attr($atts['width']); ?>; height: <?php echo esc_attr($atts['height']); ?>;"></div>
<script type="text/javascript">
function initMap() {
var mapOptions = {
center: new google.maps.LatLng(<?php echo esc_js($atts['latitude']); ?>, <?php echo esc_js($atts['longitude']); ?>),
zoom: <?php echo intval($atts['zoom']); ?>
};
var map = new google.maps.Map(document.getElementById('<?php echo esc_js($map_id); ?>'), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initMap);
</script>
<?php
return ob_get_clean();
}
add_shortcode('google_map', 'wp_dudecom_google_maps_shortcode');
?>
Instructions
File Location: Add the code to your theme's functions.php
file or a custom plugin file.
Prerequisites:
- Ensure you have a valid Google Maps API key. You can obtain this from the Google Cloud Platform.
Implementation Steps:
- Open your WordPress Dashboard: Log in to your WordPress admin panel.
- Access Theme Editor: Navigate to Appearance > Theme Editor. If you prefer using a plugin, go to Plugins > Add New and search for a plugin that allows custom code snippets.
- Edit functions.php: In the Theme Editor, locate and open the
functions.php
file from the right-hand side file list. - Insert the Code: Copy and paste the provided code into the
functions.php
file. Ensure you replace'YOUR_GOOGLE_MAPS_API_KEY'
with your actual API key. - Save Changes: Click the Update File button to save your changes.
- Use the Shortcode: In your WordPress post or page editor, insert the shortcode
[google_map]
where you want the map to appear. Customize it with attributes likelatitude
,longitude
,zoom
,width
, andheight
if needed.
Example Usage:
[google_map latitude="40.7128" longitude="-74.0060" zoom="12" width="600px" height="450px"]
This will display a Google Map centered on the specified latitude and longitude, with the desired zoom level and dimensions.
If you need assistance with implementation or more advanced functionality, consider reaching out to wp-dude.com for expert help.