# ACF Google Maps API Key From wp-config.php

> Gives Advanced Custom Fields' Google Map field your API key from a constant in wp-config.php, so the key never sits in the database or theme files.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: acf, google-maps, api-key
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/acf-google-maps-key-constant/

## Code

```php
add_filter( 'acf/fields/google_map/api', function ( $api ) {
    if ( defined( 'WPIDE_GOOGLE_MAPS_KEY' ) && WPIDE_GOOGLE_MAPS_KEY ) {
        $api['key'] = WPIDE_GOOGLE_MAPS_KEY;
    }

    return $api;
} );

add_action( 'acf/init', function () {
    if ( defined( 'WPIDE_GOOGLE_MAPS_KEY' ) && WPIDE_GOOGLE_MAPS_KEY && function_exists( 'acf_update_setting' ) ) {
        acf_update_setting( 'google_api_key', WPIDE_GOOGLE_MAPS_KEY );
    }
} );
```

## Notes

Add define( 'WPIDE_GOOGLE_MAPS_KEY', 'your-key' ); to wp-config.php and restrict the key to your domain in Google Cloud.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "ACF Google Maps API Key From wp-config.php" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
