Skip to content

Instantly share code, notes, and snippets.

@hitautodestruct
Last active February 20, 2017 12:51
Show Gist options
  • Save hitautodestruct/8912648 to your computer and use it in GitHub Desktop.
Save hitautodestruct/8912648 to your computer and use it in GitHub Desktop.
Manage Javascript dependencies in wordpress.
<?php
function register_scripts() {
if (!is_admin()){
wp_deregister_script('jquery'); // Lets use the most modern version rather than the one packaged with Wordpress
wp_deregister_script( 'l10n' ); // Unneccessary http request made by WP
// Add scripts to this array as neccessary
$scripts = array(
'jquery' => array(
'url' => 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',
'dependencies' => false,
'version' => '1.10',
'in_footer' => true
),
'jcarousellite' => array(
'url' => get_bloginfo('template_directory').'/js/libs/jcarousellite.min.js',
'dependencies' => false,
'version' => '1',
'in_footer' => true
),
'main' => array(
'url' => get_bloginfo('template_directory').'/js/script.js',
'dependencies' => array('jquery', 'jcarousellite'),
'version' => get_file_version($scripts['script']['url']),
'in_footer' => true,
'params' => array( 'Nonce' => wp_create_nonce( 'my-nonce' ) )
)
);
// Register and enqueue the above scripts
foreach($scripts as $key => $val){
if ( $val != '')
wp_register_script($key, $val['url'], $val['dependencies'], $val['version'], $val['in_footer']);
if ( isset( $val['params'] ) ){
wp_localize_script( $key, $key . 'ScriptParams', $val['params'] );
}
wp_enqueue_script($key);
}
}
}
add_action('wp_enqueue_scripts', 'register_scripts');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment