Skip to content

Instantly share code, notes, and snippets.

@jakebellacera
Last active January 21, 2016 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakebellacera/8950261 to your computer and use it in GitHub Desktop.
Save jakebellacera/8950261 to your computer and use it in GitHub Desktop.
Scripts List - PHP managed scripts list
<?php
// Default scripts
$script_list = array(
'vendor/jquery.js',
'vendor/bootstrap.js',
'vendor/video.js',
'`videojs.options.flash.swf = "' . get_bloginfo('stylesheet_directory') . '/assets/swf/videojs.swf"',
'global.js'
);
// Add custom scripts to your page
//
// Usage:
// Set a var named $scripts as an array with path (relative from either the
// /assets/css dir or as a separate URL for external scripts) into the
// array in the order that you want. Code snippets can be added as well as
// long as the string starts with a backtick (`).
//
// Example:
// $scripts = array('foo.js', '//bar.com/baz.js');
//
// Additonal Features:
// Removing Scripts - If you want to remove a script that, you can do so by
// prepending your script name with a bang ("!").
//
// Example:
// $script = array('foo.js', '!bar.js');
//
// The example above will add the script "foo.js" and
// remove the script "bar.js" if it was already going
// to be loaded.
if (isset($scripts)) {
foreach ($scripts as $script) {
if (preg_match('/^\!/', $script)) {
$index = array_search(substr($script,1), $script_list);
if ($index) {
unset($script_list[$index]);
}
} else {
array_push($script_list, $script);
}
}
}
foreach ($script_list as $script) {
if (preg_match('/^`/', $script)) {
echo '<script type="text/javascript">' . substr($script, 1) . '</script>';
} else {
if (preg_match('/^((http(s?):)?)\/\//', $script)) {
echo "<script type=\"text/javascript\" src=\"$script\"></script>\n";
} else {
echo '<script type="text/javascript" src="'. get_bloginfo('stylesheet_directory') .'/assets/js/'. $script ."\"></script>\n";
}
}
}
?>
@chrismccoy
Copy link

instead of echoing script tags, be better off using wp_enqueue_script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment