mustardBees/cmb_field_map

Google Maps API key now required for admin

Opened this issue ยท 15 comments

As per this article. This plugin no longer works until it's updated to allow an API key to be added.

https://googlegeodevelopers.blogspot.co.uk/2016/06/building-for-scale-updates-to-google.html

Great plugin though, I hope to be able to continue using it!

Thanks for the feedback @chrisdrake.

My initial thoughts for passing in the key include:

  • wp-config.php define
  • Pass through a key with each field's arguments
  • A settings page

I'm leaning towards defining the key. It's clearly not as user friendly as a settings page. However, it's my view that CMB2 and this field are already developer orientated. Any thoughts/strong opinions?

Thanks for the reply :)

I'd be more than happy to have it developer defined key because I fully agree with your point about CMB2. However I'd prefer to see it in a config file in the plugin, or perhaps in a pluggable function that we can include in our plugin or theme? I'm not too keen on it being in wp-config.php because that won't travel with the theme or plugin if it's moved.

I agree that CMB2 and this field are developer oriented and so the key should be defined in the source code. Maybe using a WordPress filter?

I like the filter approach, for the reasons chrisdrake outlined.. Doesn't make sense in a field's arguments, since the key is added just once, when the api script is registered.

This basic implementation seems to work:

    public function setup_admin_scripts() {
        $api_url = '//maps.googleapis.com/maps/api/js?libraries=places';
        $api_key = apply_filters( 'pw-google-maps-api-key', '' );
        if ( ! empty( $api_key ) ) {
            $api_url .= '&key=' . $api_key;
        }
        wp_register_script( 'pw-google-maps-api', $api_url, null, null );
        wp_enqueue_script( 'pw-google-maps', plugins_url( 'js/script.js', __FILE__ ), array( 'pw-google-maps-api' ), self::VERSION );
        wp_enqueue_style( 'pw-google-maps', plugins_url( 'css/style.css', __FILE__ ), array(), self::VERSION );
    }

I can add my API key by dropping this in a plugin:

add_filter( 'pw-google-maps-api-key', function() {
    return 'MY_API_KEY';
});

That looks absolutely perfect and exactly the sort of thing I was rambling on about ;)

sorry,

where do i put this code in order to fix maps?

That code prevents the lookup from working but the map does load and doens't come up with the javascript error.

so? where i must put this code?

To fix this issue, i updated the method setup_admin_scripts Please check my pull request above

just you can add in your functions.php the below code

 // add the plugin's google map api key 
     add_filter('os-pw-google-maps-api-key', function(){
     return 'APIKEY';
});

Note : head to google map to get api key if you don't have one

thank you very much

For the lookup / autocomplete to work, you need to enable the Google Places API in your google api console.

More info: http://stackoverflow.com/a/38297390

phh commented

While we wait for this to get merged in here is a little filter that might help you:

add_filter( 'cmb2_render_pw_map', function() {
	wp_deregister_script( 'pw-google-maps-api' );
	wp_register_script( 'pw-google-maps-api', '//maps.googleapis.com/maps/api/js?libraries=places&key=<API-KEY>', null, null );
}, 12 );

Perfect @phh ;)

Thanks it has fixed my issue too!!!