Skip to content

Instantly share code, notes, and snippets.

@abaicus
Created March 31, 2016 08:22
Show Gist options
  • Save abaicus/8083f84a418785a855eac00d9b2ec4cd to your computer and use it in GitHub Desktop.
Save abaicus/8083f84a418785a855eac00d9b2ec4cd to your computer and use it in GitHub Desktop.
Function to check if bootstrap is already included in the active theme. If it is not, it includes it.
<?php
/* CHECK FOR BOOTSTRAP (or other scripts)
There is no foolproof way to do this check. For this to work, I've assumed that the handles or at least the file names
of the enqueued scripts contain the string we need to check for.
*/
function check_dependencies() {
//Save the enqueued styles array.
$wp_styles = wp_styles();
//Dive deeper in the array to the registered scripts.
$registered_styles = $wp_styles->registered;
//Serialize the array and save it as string.
$serialized_styles = serialize($registered_styles);
//Same as above for the scripts array.
$wp_scripts = wp_scripts();
$registered_scripts = $wp_scripts->registered;
$serialized_scripts = serialize($registered_scripts);
/* Check for twitter Bootstrap and enqueue it in absence.
This is just a rudimentary check where I've assumed that both bootstrap.js and bootstrap.css are usually included in the theme.
You could adapt this to check independently for bootstrap.js (in $serialized_scripts) and bootstrap.css (in $serialized_styles).
*/
if ((strpos($serialized_scripts, 'bootstrap') == false) && (strpos($serialized_styles, 'bootstrap') == false)) {
wp_register_style( 'team_plugin_bootstrap', plugin_dir_url( __FILE__ ) . 'public/css/bootstrap.min.css', false, 'v3.3.6' );
wp_enqueue_style( 'team_plugin_bootstrap' );
}
/* Check for Fontawesome and enqueue it in absence.
As another example, to make searches more certain, you can use different strings that might be found in the file names or
the handles of the registered scripts / styles.
*/
if((strpos($serialized_styles, 'fontawesome') == false) && (strpos($serialized_styles, 'font-awesome') == false)) {
wp_register_style( 'team_plugin_fontawesome', plugin_dir_url( __FILE__ ) . 'public/css/font-awesome.min.css', false, 'v4.5.0zz' );
wp_enqueue_style( 'team_plugin_fontawesome' );
}
}
add_action( 'wp_enqueue_scripts', 'check_dependencies', 9999 ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment