Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Maps API key now required for admin #44

Open
chrisdrake opened this issue Jun 25, 2016 · 15 comments
Open

Google Maps API key now required for admin #44

chrisdrake opened this issue Jun 25, 2016 · 15 comments

Comments

@chrisdrake
Copy link

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!

@eriktelepovsky
Copy link

+1

@mustardBees
Copy link
Owner

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?

@chrisdrake
Copy link
Author

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.

@eriktelepovsky
Copy link

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?

@jbrinley
Copy link

jbrinley commented Jul 4, 2016

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

@chrisdrake
Copy link
Author

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

@themaniac
Copy link

sorry,

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

@yeahsmaggy
Copy link

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

@themaniac
Copy link

so? where i must put this code?

@oaattia
Copy link

oaattia commented Jul 19, 2016

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

@themaniac
Copy link

thank you very much

@jordif
Copy link

jordif commented Jul 24, 2016

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
Copy link

phh commented Jan 18, 2017

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

@eriktelepovsky
Copy link

Perfect @phh ;)

@Oscah3
Copy link

Oscah3 commented May 8, 2017

Thanks it has fixed my issue too!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants