Created
July 25, 2013 17:14
-
-
Save jkereako/6081838 to your computer and use it in GitHub Desktop.
This function uses jQuery getScript() to load external JavaScript files, which are printed at the footer of the HTML source. You use it by filtering nesn_load_external_script().
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
/** | |
* Hookable filter which loads external scripts in the footer. See the example below | |
* | |
* <code> | |
* filter_nesn_load_external_script( $params ) { | |
* $params['unique_id']['url'] = 'http://path.to/resource.js'; | |
* $params['unique_id']['callback'] = 'callMeAfterScriptLoads'; | |
* return $params; | |
* } | |
* | |
* add_filter( 'nesn_load_external_script', $func, 10, 1 ); | |
* </code> | |
* @uses apply_filters, esc_url | |
* @action wp_footer | |
*/ | |
function nesn_external_script_loader() { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
<?php | |
$scripts = array(); | |
$scripts = apply_filters( 'nesn_load_external_script', $scripts ); | |
if ( ! empty( $scripts ) ) : ?> | |
<?php foreach( $scripts as $script ) : ?> | |
<?php if ( ! isset( $script['url'] ) ) { continue; } ?> | |
jQuery.getScript("<?php echo esc_url( $script['url'], array('http', 'https'), 'not for display' ); ?>"<?php echo ( empty ( $script['callback'] ) ) ? '' : ', '. $script['callback']; ?>); | |
<?php endforeach; ?> | |
<?php endif; ?> | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'wp_footer', 'nesn_external_script_loader', 100, 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment