Advanced Custom Fields (ACF) and Secure Custom Fields (SCF) offer a fantastic feature: the ability to select a location using the Google Maps API. However, to use this API, you need a Google Cloud API key with the Maps JavaScript API and Geocoding API services enabled.

A common question arises: How to connect the API key to a WordPress site? The answer is below.

Getting an API key

How to get a Google Maps API Key.

Setting Up the API Key

Simply copy the following code and paste it into your theme’s functions.php file. Replace your-api-key with your actual API key.

// Sets ACF/SCF Goolge Maps API key. add_filter( 'acf/fields/google_map/api', function ( $api ) { $api[ 'key' ] = 'your-api-key'; return $api; });

If you are using Secure Custom Fields or ACF Pro, you can use this method instead of the code above (this approach is preferred):

// Sets ACF/SCF Goolge Maps API key. add_action( 'acf/init', function () { acf_update_setting( 'google_api_key', 'your-api-key' ); });

Useful Resources