My WordPress, Bootstrap theme is not working with JavaScript

I’m working on WordPress theme using Bootstrap on localhost.
I have load the CSS files correctly and I have load the js files using the right way but for some reasons js scripts is not working.

Here is my index.php

Read More
<?php get_header(); ?>


<?php get_footer(); ?>

Here is my header.php

<!DOCTYPE html>

<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href = "<?php bloginfo(stylesheet_url); ?>" rel = "stylesheet">

</head>


<body>

    <!-- Navigation -->
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Start Bootstrap</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="#about">About</a>
                    </li>
                    <li>
                        <a href="#services">Services</a>
                    </li>
                    <li>
                        <a href="#contact">Contact</a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>

Here is my footer.php

<script src= "<?php get_template_directory(); ?>/js/bootstrap.js"></script>

</body>
</html>

This problem drives me CRAZY!
I spent 2 hours searching for solution but nothing!

So what is the problem guys?

Related posts

3 comments

  1. You can use it for including your scripts

    function WM_Scripts(){
    
        wp_register_script( 'jScript', 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js' );
        wp_register_script('bootstrapjs',get_template_directory_uri() .'/js/bootstrap.min.js');
        wp_enqueue_script( 'jScript' );
        wp_enqueue_script( 'bootstrapjs' );
    
    }
    add_action( 'wp_enqueue_scripts', 'WM_Scripts' );
    

    And For stylesheet you have to use this function.

    function WM_CSS(){
        wp_register_style( 'bootstrapGrid', get_template_directory_uri() . '/css/bootstrap.min.css' );
    
        wp_enqueue_style( 'bootstrapGrid' );
    
    }
    add_action( 'wp_enqueue_scripts', 'WM_CSS' );
    

    Please refer wordpress documentation for include your script into footer.
    This the right way to include your script and styles.
    And please add wp_head(); functions in your header to load script and styles.

  2. Put this code in your “functions.php” file to enqueue “/css/bootstrap.min.css”, “/style.css” and your “/js/bootstrap.js”:

    /**
     * Enqueue scripts and styles
     */
    function theme_scripts() {
        wp_enqueue_style( 'bootstrapcss', get_template_directory_uri() . '/css/bootstrap.min.css' );
        wp_enqueue_style( 'stylesheet', get_stylesheet_uri() );
        wp_enqueue_script(
                'bootstrapjs',
                get_stylesheet_directory_uri() . '/js/bootstrap.js',
                array(),
                '3.3.5', // Bootstrap version number
                true // Place before </body>
        );
    }
    add_action( 'wp_enqueue_scripts', 'theme_scripts' );
    

    Keep note, you need to set the last parameter of wp_enqueue_script to true to place your ‘/js/bootstrap.js’ before the </body> end tag. For more info visit this link.

  3. You missed echo. You must print what the function get_template_directory_uri() returns like here:

    <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/bootstrap.js"></script>

    After that your js will work flawless.

Comments are closed.