Last active
October 4, 2016 07:58
-
-
Save dotZak/53c7a34db9c1bb0f76f93746c407fd2e to your computer and use it in GitHub Desktop.
wp_enqueue_script() filters
This file contains 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
<?php | |
/** | |
* Functions used be the WordPress theme. | |
*/ | |
if ( ! function_exists( 'get_google_maps_src' ) ) : | |
/** | |
* Take a Google Maps API v3 key (and optional callback) and return value for the 'src' attribute to load the api script. | |
* To be used for getting the wp_enqueue_script() URI parameter. | |
* | |
// Sample Implementation | |
// wp_enqueue_script( 'map_page_scripts', get_google_maps_src( 'AIzaSyCvqaM_Z5_s3BsCYcm268IBzR62JoiW3ko', 'initGoogleMaps' ), array(), false, true ); | |
* | |
* @param string $key A Google Maps API v3 key generated for this site. | |
* @param string $callback A JavaScript function name for the Google Maps API to call back to initialize the maps on the page. | |
* @return string An escaped URL for use by a wp_enqueue_script() function. | |
*/ | |
function get_google_maps_src( $key = '', $callback = '' ) | |
{ | |
if ( ! isset( $key ) ) | |
return; | |
$callback = ( isset( $callback ) ) ? '&callback=' . $callback : ''; | |
return esc_url_raw( sprintf( 'https://maps.googleapis.com/maps/api/js?key=%1$s%2$s', $key, $callback ) );; | |
} | |
endif; | |
if ( ! function_exists( 'add_async_attribute' ) ) : | |
/** | |
* Add 'async' and 'defer' attributs to the html 'script' tag. | |
* | |
* @param string $tag The raw html $tag as it is received. | |
* @param string $handle The $handle of the current script, e.g. 'rushhour_map_page_scripts' | |
* @return string A string which has conditionally had 'async' and 'defer' attributes added to it. | |
*/ | |
function add_async_attribute( $tag, $handle ) | |
{ | |
$async_scripts = array( 'map_page_scripts' ); | |
if ( ! in_array( $handle, $async_scripts ) ) | |
return $tag; | |
return str_replace( ' src', ' async defer src', $tag ); | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment