Home » Wordpress » Adding JavaScript files to a WordPress Theme

Adding JavaScript files to a WordPress Theme

Posted by: admin November 30, 2017 Leave a comment

Questions:

It is my first time in creating a WordPress Theme using Bootstrap 3.0, when the files where just plain HTML, everything was functioning correctly that is until I converted it into a WordPress theme.

The JavaScript is not loading properly, I am unsure if I should just plainly call through either header.php or footer.php

Below are the code I used.

header.php

<head>
    <meta charset="UTF-8">
    <title>Home</title>
    <link href="<?php bloginfo('stylesheet_url');?>" rel="stylesheet">
    <?php wp_enqueue_script("jquery"); ?>
    <?php wp_head(); ?>
</head>

index.php

<?php get_header(); ?>
        <!--carousel-->
        <div class="container full-width">
            <div id="myCarousel" class="carousel slide">
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#myCarousel" data-slide-to="1"></li>
                    <li data-target="#myCarousel" data-slide-to="2"></li>
                </ol>
                <div class="carousel-inner">
                    <div class="item active">
                        <img src="img/1.jpg" alt="Slide-0">
                        <div class="cap-right slide-cap">
                            <img src="img/slide1cap.png">
                            <a href="#">View Lookbook</a>
                        </div>
                    </div>
                    <div class="item">
                        <img src="img/2.jpg" alt="Slide-1"> 
                        <div class="cap-right slide-cap">
                            <img src="img/slide2cap.png" alt="SlideCap">
                            <a href="#">View More</a>
                        </div>
                    </div>
                    <div class="item">
                        <img src="img/3.jpg" alt="Slide-2">
                        <div class="cap-left slide-cap">
                            <img src="img/slide3cap.png" alt="SlideCap">
                            <a href="#">Watch Video</a>
                        </div>
                    </div>
                    <div class="item">
                        <img src="img/4.jpg" alt="Slide-3">
                        <div class="cap-left slide-cap">
                            <img src="img/slide4Cap.png" alt="SlideCap">
                            <a href="#">Shop Shirts Now</a>
                        </div>
                    </div>
                </div>
                <!--arrows-->
                <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                </a>
                <a class="right carousel-control" href="#myCarousel" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                </a>
            </div>
        </div>
        <!--end of carousel-->
<?php get_footer(); ?>

functions.php

<?php 

function wpbootstrap_scripts_with_jquery()
{
    // Register the script like this for a theme:
    wp_register_script( 'custom-script', get_template_directory_uri() . 'js/bootstrap.min.js', array( 'jquery' ) );
    // For either a plugin or a theme, you can then enqueue the script:
    wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );

?>
Answers:

All you need do is change

js/bootstrap.min.js

to

/js/bootstrap.min.js

Note: you need to view page source (from your browser) to confirm if the path to the bootstrap.min.js is correct, else it wont work.

Questions:
Answers:

This is how I normally would register the scripts on my WordPress installs.

<?php 
    function wpbootstrap_scripts_with_jquery()
    {
        wp_deregister_script( 'bootstrap-min-js' );
        wp_register_script( 'bootstrap-min-js', get_bloginfo('template_directory') . '/js/bootstrap.min.js', array( 'jquery' ) );
        wp_enqueue_script( 'bootstrap-min-js' );
    }
    add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );

    ?>

Questions:
Answers:

write this wp_enqueue_script("jquery"); after the wp_head();

then it should work.