cmb_field_map icon indicating copy to clipboard operation
cmb_field_map copied to clipboard

Google Maps API key now required for admin

Open chrisdrake opened this issue 8 years ago • 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!

chrisdrake avatar Jun 25 '16 20:06 chrisdrake

+1

eriktelepovsky avatar Jun 29 '16 10:06 eriktelepovsky

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?

mustardBees avatar Jun 29 '16 12:06 mustardBees

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.

chrisdrake avatar Jun 29 '16 12:06 chrisdrake

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?

eriktelepovsky avatar Jun 29 '16 12:06 eriktelepovsky

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';
});

jbrinley avatar Jul 04 '16 15:07 jbrinley

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

chrisdrake avatar Jul 04 '16 15:07 chrisdrake

sorry,

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

themaniac avatar Jul 15 '16 16:07 themaniac

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

yeahsmaggy avatar Jul 19 '16 13:07 yeahsmaggy

so? where i must put this code?

themaniac avatar Jul 19 '16 14:07 themaniac

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

oaattia avatar Jul 19 '16 15:07 oaattia

thank you very much

themaniac avatar Jul 20 '16 12:07 themaniac

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

jordif avatar Jul 24 '16 21:07 jordif

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 );

phh avatar Jan 18 '17 08:01 phh

Perfect @phh ;)

eriktelepovsky avatar Jan 18 '17 09:01 eriktelepovsky

Thanks it has fixed my issue too!!!

Oscah3 avatar May 08 '17 13:05 Oscah3