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
<?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?
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
<?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?
Share Improve this question asked Jul 13, 2015 at 12:56 Abbas AleidAbbas Aleid 651 silver badge10 bronze badges 4-
2
Use the proper enqueue (
wp_enqueue_script
) functions, don't manually add script tags - it's a bad habit – ɴᴀᴛᴇ ᴅᴇᴠ Commented Jul 13, 2015 at 13:08 - Can you explain to me how to use it? – Abbas Aleid Commented Jul 13, 2015 at 13:40
-
I have load the CSS files correctly
, how are you so sure? Because for what I know you didn't – Ron van der Heijden Commented Jul 13, 2015 at 14:05 - I can know, because CSS changes appears in my website. – Abbas Aleid Commented Jul 13, 2015 at 14:12
3 Answers
Reset to default 4You can use it for including your scripts
function WM_Scripts(){
wp_register_script( 'jScript', 'https://ajax.googleapis./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.
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.
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.