问题
We've been developing a Wordpress theme and stumbled on a weird problem. A popup displayed by bootstrap.js appears for a fraction of a second and then disappears. After thorough research I have figured out that the problem is caused by a plugin which also uses bootstrap.js. Two bootstrap.js were loaded, one from our theme and another from the plugin. How to avoid this conflict?
回答1:
The solution is to check if bootstrap.js is already enqueued. I've added the following to functions.php of the theme:
function enqueue_scripts() {
// Check if bootstrap is already there
$bootstrap_enqueued = FALSE;
foreach( $wp_scripts->registered as $script ) {
if ((stristr($script->src, 'bootstrap.min.js') !== FALSE or
stristr($script->src, 'bootstrap.js') != FALSE) and
wp_script_is($script->handle, $list = 'enqueued')) {
$bootstrap_enqueued = TRUE;
break;
}
}
if (!$bootstrap_enqueued) {
wp_enqueue_script( 'theme-bootstrap-js', get_template_directory_uri() . '/js/bootstrap.min.js', array(), '2015', true );
}
// Note the last parameter. It's important to be the last in the list of hooks
add_action( 'wp_enqueue_scripts', 'enqueue_scripts', PHP_INT_MAX );`
来源:https://stackoverflow.com/questions/35077849/how-to-avoid-double-inclusion-of-bootstrap-js