JavaScript clean up
- Modernizr is now enqueued and included after stylesheets - jQuery is no longer hardcoded since WP now supports protocol-relative URLs - jQuery fallback output is handled by a new function in lib/scripts.php (the method is still the same one from HTML5 Boilerplate)
This commit is contained in:
@@ -87,8 +87,8 @@ If you're using LESS, make sure you compile the files to the proper locations:
|
|||||||
|
|
||||||
JavaScript is loaded in the following order:
|
JavaScript is loaded in the following order:
|
||||||
|
|
||||||
1. `/theme/assets/js/vendor/modernizr-2.6.2.min.js` (in `head.php`)
|
1. `/theme/assets/js/vendor/modernizr-2.6.2.min.js`
|
||||||
2. `jquery-1.9.0.min.js` via Google CDN with local fallback (in `head.php`)
|
2. `jquery-1.9.0.min.js` via Google CDN with local fallback
|
||||||
3. `/theme/assets/js/plugins.js`
|
3. `/theme/assets/js/plugins.js`
|
||||||
4. `/theme/assets/js/main.js`
|
4. `/theme/assets/js/main.js`
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@
|
|||||||
* 4. /child-theme/style.css (if a child theme is activated)
|
* 4. /child-theme/style.css (if a child theme is activated)
|
||||||
*
|
*
|
||||||
* Enqueue scripts in the following order:
|
* Enqueue scripts in the following order:
|
||||||
* 1. /theme/assets/js/vendor/modernizr-2.6.2.min.js (in head.php)
|
* 1. /theme/assets/js/vendor/modernizr-2.6.2.min.js
|
||||||
* 2. jquery-1.9.0.min.js via Google CDN (in head.php)
|
* 2. jquery-1.9.0.min.js via Google CDN
|
||||||
* 3. /theme/assets/js/plugins.js
|
* 3. /theme/assets/js/plugins.js
|
||||||
* 4. /theme/assets/js/main.js
|
* 4. /theme/assets/js/main.js
|
||||||
*/
|
*/
|
||||||
@@ -26,21 +26,44 @@ function roots_scripts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// jQuery is loaded in header.php using the same method from HTML5 Boilerplate:
|
// jQuery is loaded in header.php using the same method from HTML5 Boilerplate:
|
||||||
// Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline
|
// Grab Google CDN's latest jQuery with a protocol relative URL; fall back to local if offline
|
||||||
// It's kept in the header instead of footer to avoid conflicts with plugins.
|
// It's kept in the header instead of footer to avoid conflicts with plugins.
|
||||||
if (!is_admin()) {
|
if (!is_admin()) {
|
||||||
wp_deregister_script('jquery');
|
wp_deregister_script('jquery');
|
||||||
wp_register_script('jquery', '', '', '1.9.0', false);
|
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js', array('modernizr'), null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_single() && comments_open() && get_option('thread_comments')) {
|
if (is_single() && comments_open() && get_option('thread_comments')) {
|
||||||
wp_enqueue_script('comment-reply');
|
wp_enqueue_script('comment-reply');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wp_register_script('modernizr', get_template_directory_uri() . '/assets/js/vendor/modernizr-2.6.2.min.js', false, null, false);
|
||||||
wp_register_script('roots_plugins', get_template_directory_uri() . '/assets/js/plugins.js', false, null, false);
|
wp_register_script('roots_plugins', get_template_directory_uri() . '/assets/js/plugins.js', false, null, false);
|
||||||
wp_register_script('roots_main', get_template_directory_uri() . '/assets/js/main.js', false, null, false);
|
wp_register_script('roots_main', get_template_directory_uri() . '/assets/js/main.js', false, null, false);
|
||||||
|
wp_enqueue_script('modernizr');
|
||||||
|
wp_enqueue_script('jquery');
|
||||||
wp_enqueue_script('roots_plugins');
|
wp_enqueue_script('roots_plugins');
|
||||||
wp_enqueue_script('roots_main');
|
wp_enqueue_script('roots_main');
|
||||||
}
|
}
|
||||||
|
|
||||||
add_action('wp_enqueue_scripts', 'roots_scripts', 100);
|
add_action('wp_enqueue_scripts', 'roots_scripts', 100);
|
||||||
|
|
||||||
|
// http://wordpress.stackexchange.com/a/12450
|
||||||
|
function roots_jquery_local_fallback($src, $handle) {
|
||||||
|
static $add_jquery_fallback = false;
|
||||||
|
|
||||||
|
if ($add_jquery_fallback) {
|
||||||
|
echo '<script>window.jQuery || document.write(\'<script src="' . get_template_directory_uri() . '/assets/js/vendor/jquery-1.9.0.min.js"><\/script>\')</script>' . "\n";
|
||||||
|
$add_jquery_fallback = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($handle === 'jquery') {
|
||||||
|
$add_jquery_fallback = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $src;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_admin()) {
|
||||||
|
add_filter('script_loader_src', 'roots_jquery_local_fallback', 10, 2);
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,11 +8,6 @@
|
|||||||
<title><?php wp_title('|', true, 'right'); bloginfo('name'); ?></title>
|
<title><?php wp_title('|', true, 'right'); bloginfo('name'); ?></title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
|
||||||
<script src="<?php echo get_template_directory_uri(); ?>/assets/js/vendor/modernizr-2.6.2.min.js"></script>
|
|
||||||
|
|
||||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
|
|
||||||
<script>window.jQuery || document.write('<script src="<?php echo get_template_directory_uri(); ?>/assets/js/vendor/jquery-1.9.0.min.js"><\/script>')</script>
|
|
||||||
|
|
||||||
<?php wp_head(); ?>
|
<?php wp_head(); ?>
|
||||||
|
|
||||||
<link rel="alternate" type="application/rss+xml" title="<?php echo get_bloginfo('name'); ?> Feed" href="<?php echo home_url(); ?>/feed/">
|
<link rel="alternate" type="application/rss+xml" title="<?php echo get_bloginfo('name'); ?> Feed" href="<?php echo home_url(); ?>/feed/">
|
||||||
|
|||||||
Reference in New Issue
Block a user