Include external JavaScript into Roots

Published on 8th Apr 2013
a PHP plush on top of a laptopa PHP plush on top of a laptop
PHP

Include the JavaScript file into wp-content/themes/roots/assets/js

Then, open scripts.php inside wp-content/themes/roots/lib/

1<?php
2/**
3 * Enqueue scripts and stylesheets
4 *
5 * Enqueue stylesheets in the following order:
6 * 1. /theme/assets/css/bootstrap.css
7 * 2. /theme/assets/css/bootstrap-responsive.css
8 * 3. /theme/assets/css/app.css
9 * 4. /child-theme/style.css (if a child theme is activated)
10 *
11 * Enqueue scripts in the following order:
12 * 1. jquery-1.9.1.min.js via Google CDN
13 * 2. /theme/assets/js/vendor/modernizr-2.6.2.min.js
14 * 3. /theme/assets/js/plugins.js (in footer)
15 * 4. /theme/assets/js/main.js (in footer)
16 */
17function roots_scripts() {
18 wp_enqueue_style('roots_bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.css', false, null);
19 wp_enqueue_style('roots_bootstrap_responsive', get_template_directory_uri() . '/assets/css/bootstrap-responsive.css', array('roots_bootstrap'), null);
20 wp_enqueue_style('roots_app', get_template_directory_uri() . '/assets/css/app.css', false, null);
21
22 // Load style.css from child theme
23 if (is_child_theme()) {
24 wp_enqueue_style('roots_child', get_stylesheet_uri(), false, null);
25 }
26
27 // jQuery is loaded using the same method from HTML5 Boilerplate:
28 // Grab Google CDN's latest jQuery with a protocol relative URL; fallback to local if offline
29 // It's kept in the header instead of footer to avoid conflicts with plugins.
30 if (!is_admin() && current_theme_supports('jquery-cdn')) {
31 wp_deregister_script('jquery');
32 wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', false, null, false);
33 add_filter('script_loader_src', 'roots_jquery_local_fallback', 10, 2);
34 }
35
36 if (is_single() && comments_open() && get_option('thread_comments')) {
37 wp_enqueue_script('comment-reply');
38 }
39
40 wp_register_script('modernizr', get_template_directory_uri() . '/assets/js/vendor/modernizr-2.6.2.min.js', false, null, false);
41 wp_register_script('roots_plugins', get_template_directory_uri() . '/assets/js/plugins.js', false, null, true);
42 wp_register_script('roots_main', get_template_directory_uri() . '/assets/js/main.js', false, null, true);
43 //register external scripts
44 wp_register_script('backstretch', '/assets/js/vendor/jquery.backstretch.min.js', false, null, false);
45
46 wp_enqueue_script('jquery');
47 wp_enqueue_script('modernizr');
48 wp_enqueue_script('roots_plugins');
49 wp_enqueue_script('roots_main');
50 wp_enqueue_script('backstretch');
51}
52add_action('wp_enqueue_scripts', 'roots_scripts', 100);
53
54// http://wordpress.stackexchange.com/a/12450
55function roots_jquery_local_fallback($src, $handle) {
56 static $add_jquery_fallback = false;
57
58 if ($add_jquery_fallback) {
59 echo '<script>window.jQuery || document.write(\'<script src="' . get_template_directory_uri() . '/assets/js/vendor/jquery-1.9.1.min.js"><\/script>\')</script>' . "\n";
60 $add_jquery_fallback = false;
61 }
62
63 if ($handle === 'jquery') {
64 $add_jquery_fallback = true;
65 }
66
67 return $src;
68}
69
70function roots_google_analytics() { ?>
71<script>
72 var _gaq=[['_setAccount','<?php echo GOOGLE_ANALYTICS_ID; ?>'],['_trackPageview']];
73 (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
74 g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
75 s.parentNode.insertBefore(g,s)}(document,'script'));
76</script>
77<?php }
78if (GOOGLE_ANALYTICS_ID) {
79 add_action('wp_footer', 'roots_google_analytics', 20);
80}
81
82// damn

That should do the trick!

This note was last updated on 11th Apr 2021,
byAjmal AfifAjmal Afif
Older note
Fake your passion

Copyright © 2009 – 2021 Ajmal Afif, All Rights Reserved.

LinkedIn iconGitHub icon