How to avoid double inclusion of Bootstrap.js?

帅比萌擦擦* 提交于 2019-12-08 09:42:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!