如何避免 Bootstrap.js 的双重包含? [英] How to avoid double inclusion of Bootstrap.js?
问题描述
我们一直在开发 Wordpress 主题,但偶然发现了一个奇怪的问题.bootstrap.js 显示的弹出窗口会出现几分之一秒然后消失.经过深入研究,我发现问题是由一个也使用 bootstrap.js 的插件引起的.加载了两个 bootstrap.js,一个来自我们的主题,另一个来自插件.如何避免这种冲突?
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?
推荐答案
解决方案是检查 bootstrap.js 是否已经入队.我在主题的functions.php中添加了以下内容:
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 );`
这篇关于如何避免 Bootstrap.js 的双重包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!