Map Field

A map field with Google Maps or OpenStreetMap.

The Map field stores a location: search for an address, click the map or drag the pin, and Modern Fields saves the coordinates along with the address, its city, postcode and country.

It works out of the box with OpenStreetMap, with no account and no API key. Add a Google Maps API key and the same field draws with Google Maps instead.

The field lives in the admin: it stores a location, it does not display a map to your visitors. Your template decides what to show — an address, a link to a map, or a map you draw yourself (see Display the location below).

Choose a map provider

One provider draws every map of the site. By default, it is chosen from your credentials: no Google key means OpenStreetMap, a Google key means Google Maps. The field settings tell you which one is in use, under Map provider.

You wantWhat to do
OpenStreetMapNothing. Do not set a Google key.
OpenStreetMap, while a Google key is set on the siteAdd define('MODERN_FIELDS_MAPS_PROVIDER', 'osm'); to the wp-config file
Google MapsSet a Google Maps API key (see below) and enable three Google APIs
Google Maps with a custom styleAlso set a Map ID
How the provider is chosen

MODERN_FIELDS_MAPS_PROVIDER accepts 'google' or 'osm' and always wins over the key. The modern-fields/maps/provider filter runs last and can override both.

PHP

Switching provider on a site that already has values is safe: coordinates and addresses stay valid. A staging site without the Google key falls back to OpenStreetMap while production uses Google: define MODERN_FIELDS_MAPS_PROVIDER on both to keep them aligned.

Google MapsOpenStreetMap
Address searchSuggestions as you typeRuns when you press Enter or click Search, up to 5 matches
Address after a clickImmediateArrives a moment after the pin, the coordinates are saved first
Place IDGoogle's place IDEmpty
SettingsAllNo map type, theme, map ID, Street View, fullscreen or clickable points of interest
CostBilled by Google beyond the free allowanceFree
What changes from one provider to the other

Use Google Maps

Set the API key

There is no settings screen for the key, on purpose: a key stored in the database travels into every export, staging copy and backup. Define it in the wp-config file:

PHP

Or return it from the modern-fields/maps/api_key filter, to read it from the environment. The filter runs after the constant, so it wins.

PHP

Enable the Google APIs

In the Google Cloud console, on the project of the key, enable these three APIs. Billing must be enabled on the project, even within the free allowance.

APIPowersWithout it
Maps JavaScript APIThe map itselfNo map: the field falls back to latitude and longitude inputs
Places API (New)The address searchThe search box says it is unavailable, the map still works
Geocoding APIThe address after a click or a dragThe pin and coordinates are saved, the address stays empty

A browser key cannot be kept secret: it is sent with Google's script. Protect it with restrictions instead: in APIs & Services › Credentials, restrict it to your website (https://yourdomain.com/*) and to these three APIs.

Style the map with a Map ID

The Theme setting switches between light and dark without any setup. For a fully custom look, create a Map ID in Google Maps Platform › Map management (type JavaScript, rendering Vector), attach a map style to it, then paste it in the field's Map ID setting, or site-wide:

PHP

Configuration

Go to Modern Fields > Fields groups and add the Map field block. Its settings are in the inspector. Settings marked Google only show when Google Maps draws the map, and keep their value while hidden.

SettingEffect
Default centerWhere the map opens while the field is empty (latitude and longitude). Paris by default.
Default zoomThe zoom level of the empty map. 10 by default.
HeightThe height of the map in the form, in px, vh or rem. 400px by default.
Map type (Google)Road map, Satellite, Terrain or Hybrid.
ZoomingCtrl + scroll (default, so a long form doesn't zoom the map as you scroll past it), Scroll, or Locked.
Theme (Google)System, Light or Dark.
Marker colorThe color of the pin.
Map ID (Google)A Map ID carrying a custom style. Overrides the site-wide one.
Search for an addressAdds a search box above the map. Turn it off to never call the search service.
Limit the search to these countriesOnly suggest addresses in the countries you pick.
ControlsShow the zoom buttons, the map type switcher (Google), the Street View pegman (Google), the fullscreen button (Google), and make points of interest clickable (Google).
The Map field settings

To keep the Google bill down, the field group builder never loads a map by itself: it shows a placeholder, with a Preview the map button on Google Maps.

Fill the field

In the post, the term, the user or any location of the group, place the pin in one of these ways:

  • Search for an address and pick a result
  • Click on the map
  • Drag the pin
  • Click Locate me to use your current position
  • Type the latitude and longitude

The address is looked up in the language of your admin. Clear empties the field.

What the field stores

A location is saved as an array. Its keys are the same as ACF's Google Map field, so a template written for ACF keeps working untouched.

KeyExample
address10 Rue de Rivoli, 75004 Paris, France
lat / lng48.8558 / 2.3588
zoom15
place_idGoogle's place ID. Empty with OpenStreetMap
nameHôtel de Ville, only when a business was picked
street_number, street_name, street_name_short10, Rue de Rivoli
city, post_codeParis, 75004
state, state_shortÎle-de-France, IDF
country, country_shortFrance, FR
The keys of a Map field value

Every value is a string, as in ACF: cast the coordinates where you need numbers. Optional keys are only filled when the provider returns them — a rural address often has no street number — so always check before printing.

Display the location

Read the value with get_field(). A field that was never filled returns an empty array.

PHP

modern_fields_get_map_url() turns the value into a link to Google Maps or openstreetmap.org, following the site's provider. It builds a plain URL: no key, no script, nothing billed. It returns null when the value has no usable location.

PHP
PHP

To draw a map on the front end, use the coordinates with the map library of your choice. With Google Maps, the key is yours to enqueue: the plugin only uses it in the admin, and every map shown to a visitor is a billable map load.

Use your own OpenStreetMap services

By default, maps come from the OpenStreetMap tile servers and addresses from Nominatim. Both are run by the OpenStreetMap Foundation under usage policies the field is built to respect: it is only used in the admin, the search runs when you ask for it, and requests are spaced. A site with heavy editing, or wanting another look, can point the field to its own services:

PHP

If the map fills with "Access denied" tiles, the browser sent no referrer: some security plugins set Referrer-Policy: no-referrer on the whole site, which OpenStreetMap refuses. Relax that policy, or use a tile service of your own.

Coming from ACF

The stored value is identical, so your templates need no change. Only the API key moves: ACF saves it in a setting, Modern Fields reads it from a constant or a filter. To reuse the key already saved in ACF:

PHP

A key created after March 2025 needs the Places API (New), which ACF does not use: enable it for the address search to work.

Constants and filters

Each filter runs after its constant, so it wins. They are also listed in Hooks: Actions & Filters.

ConstantFilterRole
MODERN_FIELDS_MAPS_PROVIDERmodern-fields/maps/providerThe provider: google or osm
MODERN_FIELDS_GOOGLE_MAPS_API_KEYmodern-fields/maps/api_keyThe Google Maps API key
MODERN_FIELDS_GOOGLE_MAPS_MAP_IDmodern-fields/maps/map_idThe site-wide Google Map ID
MODERN_FIELDS_OSM_TILE_URLmodern-fields/maps/osm/tile_urlThe OpenStreetMap tile URL
MODERN_FIELDS_OSM_GEOCODER_URLmodern-fields/maps/osm/geocoder_urlThe Nominatim-compatible geocoder
—modern-fields/maps/osm/attributionThe credit line on OpenStreetMap maps
—modern-fields/maps/providersRegister another map provider

Time Field

Color Picker Field