diff --git a/classes.fields.php b/classes.fields.php index 506b7c3c..ecffea34 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -1653,6 +1653,7 @@ public function get_default_args() { 'default_zoom' => '8', 'string-marker-title' => esc_html__( 'Drag to set the exact location', 'cmb' ), 'string-gmaps-api-not-loaded' => esc_html__( 'Google Maps API not loaded.', 'cmb' ), + 'google_api_key' => '', ) ); } @@ -1661,7 +1662,22 @@ public function enqueue_scripts() { parent::enqueue_scripts(); - wp_enqueue_script( 'cmb-google-maps', '//maps.google.com/maps/api/js?libraries=places' ); + $maps_src = '//maps.google.com/maps/api/js?libraries=places'; + + // Check for our key with either a field argument or constant. + if ( ! empty( $this->args['google_api_key'] ) ){ + $key = $this->args['google_api_key']; + } elseif ( defined( 'CMB_GAPI_KEY' ) ) { + $key = CMB_GAPI_KEY; + } + + // Only add the key argument if it's been set. + if ( ! empty( $key ) ) { + $maps_src = add_query_arg( 'key', $key, $maps_src ); + } + + // Enqueue our scripts. + wp_enqueue_script( 'cmb-google-maps', $maps_src ); wp_enqueue_script( 'cmb-google-maps-script', trailingslashit( CMB_URL ) . 'js/field-gmap.js', array( 'jquery', 'cmb-google-maps' ) ); wp_localize_script( 'cmb-google-maps-script', 'CMBGmaps', array( diff --git a/example-functions.php b/example-functions.php index eb18a71c..8339f11c 100644 --- a/example-functions.php +++ b/example-functions.php @@ -44,7 +44,7 @@ function cmb_sample_metaboxes( array $meta_boxes ) { array( 'id' => 'field-22', 'name' => 'Color', 'type' => 'colorpicker' ), - array( 'id' => 'field-23', 'name' => 'Location', 'type' => 'gmap' ), + array( 'id' => 'field-23', 'name' => 'Location', 'type' => 'gmap', 'google_api_key' => '{CUSTOM_KEY}' ), array( 'id' => 'field-24', 'name' => 'Title Field', 'type' => 'title' ), diff --git a/readme.md b/readme.md index 41443058..9cbac657 100644 --- a/readme.md +++ b/readme.md @@ -59,7 +59,7 @@ See [CONTRIBUTING.md](https://github.com/humanmade/Custom-Meta-Boxes/blob/master * Add hide_on field argument * Add Composer support * Enhancement - enable for attachments -* Fix bug with unnatached images on custom post types +* Fix bug with unattached images on custom post types * Fix error in WYSIWYG * Fix fields not getting correctly initialized if meta box is collapsed on page load * Fix bug with page-template restricted meta boxes showing if the post hasn't been saved at all.