Google Maps into WordPress website

How to Integrate Google Maps into WordPress Website

Integrating Google Maps into your WordPress website can significantly enhance its functionality and user experience. Whether you’re running a business website, a blog, or an e-commerce platform, adding a Google Map can help your visitors locate your business, visualize geographic data, or even plan their routes. 

Here’s a step-by-step guide on seamlessly integrating Google Maps into your WordPress site.

Step-by-Step Guide to Integrating Google Maps

1. Using a Plugin

The easiest way to integrate Google Maps into your WordPress site is by using a plugin. Plugins simplify the process and offer various customization options without needing to touch any code.

a. Choose a Plugin

There are several popular Google Maps plugins available for WordPress. Some of the most recommended include:

  • WP Google Maps
  • Google Maps Widget
  • MapPress Maps for WordPress

For this guide, we will use the WP Google Maps plugin as an example.

b. Install and Activate the Plugin

  1. Go to your WordPress dashboard.
  2. Navigate to Plugins > Add New.
  3. Search for WP Google Maps.
  4. Click Install Now and then Activate.

c. Configure the Plugin

  1. Once activated, go to Maps in the dashboard menu.
  2. Click Add New Map.
  3. Enter the required details, including map name, description, and address.
  4. Customize the map settings (zoom level, map type, etc.).

d. Obtain a Google Maps API Key

Google Maps requires an API key for integration:

  1. Go to the Google Cloud Platform.
  2. Create a new project or select an existing one.
  3. Navigate to APIs & Services > Credentials.
  4. Click Create Credentials > API Key.
  5. Copy the API key and go back to your WordPress dashboard.
  6. Paste the API key into the plugin settings under Maps > Settings > Advanced Settings.

e. Add the Map to Your Website

  1. Once your map is configured, you’ll see a shortcode generated for your map.
  2. Copy the shortcode.
  3. Paste the shortcode into any page or post where the map appears.

2. Manually Embedding Google Maps

If you prefer not to use a plugin, you can manually embed Google Maps using the embed code provided by Google Maps.

a. Go to Google Maps

  1. Open Google Maps.
  2. Search for the location you want to embed.

b. Get the Embed Code

  1. Click on the Share button.
  2. Select the Embed a map tab.
  3. Adjust the map size if needed.
  4. Copy the HTML embed code.

c. Add the Embed Code to Your Website

  1. Go to your WordPress dashboard.
  2. Navigate to the page or post where you want to add the map.
  3. Switch to the Text editor (or Custom HTML block if using the block editor).
  4. Paste the embed code where you want the map to appear.
  5. Save and publish your changes.

3. Using the Google Maps API

For more advanced users, integrating Google Maps using the Google Maps JavaScript API provides greater flexibility and control over the map’s appearance and functionality.

a. Obtain an API Key

  1. Follow the steps mentioned earlier to obtain a Google Maps API key.

b. Add the API Key to Your Theme

  1. Go to your WordPress dashboard.
  2. Navigate to Appearance > Theme Editor.
  3. Locate the functions.php file of your theme.
  4. Add the following code to enqueue the Google Maps API script:

function my_custom_google_maps_api() {

    wp_enqueue_script( ‘google-maps’, ‘https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY’, null, null, true );

}

add_action( ‘wp_enqueue_scripts’, ‘my_custom_google_maps_api’ );

 

Replace YOUR_API_KEY with your actual API key.

c. Add Custom JavaScript

  1. Create a custom JavaScript file (e.g., custom-maps.js) and upload it to your theme’s js folder.
  2. Enqueue the custom JavaScript file in your functions.php:

function enqueue_custom_maps_script() {

    wp_enqueue_script( ‘custom-maps’, get_template_directory_uri() . ‘/js/custom-maps.js’, array( ‘google-maps’ ), null, true );

}

add_action( ‘wp_enqueue_scripts’, ‘enqueue_custom_maps_script’ );

 

1.   Add your map initialization code in custom-maps.js:

function initMap() {

    var location = { lat: -25.344, lng: 131.036 };

    var map = new google.maps.Map(document.getElementById(‘map’), {

        zoom: 4,

        center: location

    });

    var marker = new google.maps.Marker({

        position: location,

        map: map

    });

}

2. Create a div element with the ID map in the page or post where you want the map to appear:

<div id=”map” style=”width:100%; height:400px;”></div>

3. Add the initMap function to the window’s onload event to ensure your custom JavaScript is called when the page loads.

Conclusion

Integrating Google Maps into your WordPress website can be achieved in various ways, each with its level of complexity and customization. Using a plugin is the most straightforward and user-friendly method, while manually embedding or using the API offers more control and flexibility. Choose the method that best suits your technical skills and website’s needs. With Google Maps, you can enhance your site’s functionality and provide a better experience for your visitors.

Join us and other site creators to share your latest project or get a quick advice on website building.

Live Composer is free and open-source. We invite all the users and developers to join us in plugin development.

It's super easy to create designs or extensions for Live Composer. Sell your add-ons to 30K+ plugin users.