Created
April 23, 2019 13:53
-
-
Save kostiantyn-petlia/1c71899bd7d68c0b388a77f58a06d593 to your computer and use it in GitHub Desktop.
WP/ACF - Load Google Map API if it needs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Load Google Map only if it needs (when we load ACF field type='google_map') | |
*/ | |
add_action( 'acf/init', function () { | |
acf_update_setting( 'google_api_key', ( get_theme_mod( 'google_maps_api' ) ?: 'YoUrLiCeNsECoDePlAcEhErE' ) ); | |
} ); | |
add_action( 'wp_enqueue_scripts', function () { | |
wp_register_script( 'google-map', 'https://maps.googleapis.com/maps/api/js?key=' . acf_get_setting( 'google_api_key' ) . '&v=3.exp', null, false, true ); | |
wp_register_script( 'google-map-init', get_template_directory_uri() . '/js/google-map-init.js', [ 'google-map' ], false, true ); | |
}, 5 ); | |
add_filter( 'acf/load_field/type=google_map', function ( $field ) { | |
wp_enqueue_script( 'google-map-init'); | |
return $field; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment