WordPress does not load jQuery

I'd like to use WordPress with Bootstrap but WordPress does not load jQuery properly so I get this error message:

Uncaught TypeError: Bootstrap's JavaScript requires jQuery. jQuery must be included before Bootstrap's JavaScript.

This is from my functions.php:

function add_theme_scripts() {
    wp_enqueue_style('bootstrap', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css');
    wp_enqueue_style('style', get_theme_file_path() . '/style.css');
    wp_enqueue_script('jquery');
    wp_enqueue_script('Popper', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js', array('jquery'), null, true);
    wp_enqueue_script('Bootstrap', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js', array('jquery', Popper), null, true);
    wp_enqueue_script('script', get_theme_file_path() . '/assets/js/script.js', array('jquery', 'Popper', 'Bootstrap'), null, true);
}
add_action('wp_enqueue_scripts', 'add_theme_scripts');

Could you please help me with this issue? Thanks a lot in advance

Topic twitter-bootstrap functions jquery Wordpress

Category Web


I forgot to add a default case to the switch...case statement of my add_script_attributes function in the functions.php file. Sorry that I did not see this before.

From my functions.php:

function add_script_attributes($tag, $handle) {
    switch($handle) {
        case "Popper":
            return str_replace("src='https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js'", "src='https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js' integrity='sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo' crossorigin='anonymous'", $tag);
        case "Bootstrap":
            return str_replace("src='https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js'", "src='https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js' integrity='sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI' crossorigin='anonymous'", $tag);
        default:
            return $tag;
    }
}
add_filter('script_loader_tag', 'add_script_attributes', 10, 2);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.