My JavaScript files don’t seem like it is linked to my PHP file.
I am building a WordPress website. I have header.php and calling this header.php file by including
in my main.php file. I added JS path in the head tag in header.php, but it doesn’t seem like it’s working(the JS files I added are for a carousel, but does not change anything). I just started learning so I can’t even guess what I am doing wrong here. What can I do to link my JS files?
- header.php, main.php, and js folder are in the root folder. Javascript files are in the js folder.
— This is how I linked JS file in header.php. inside of the head tag
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="text/javascript" src="js/Jquery.js"></script>
<script type="text/javascript" src="js/lightslider.js"></script>
<?php wp_head(); ?>
</head>
6
Answers
to fix this you can try adding that script at the end of the page and try this instead
just removed
from it and it will work for sure
whenever you add any JS file add some unique number function after the extension.you can use timestamp because it change everytime.
Just like following.(example is for PHP & Javascript)
Because of
cache
sometimes it happens that your code doesn’t change.also if you are windows user just press
CTRL + SHIFT + R
key to hard refresh the browser to clear cacheCan you please try to put a
slash
character in the front to make an absolute path like so:In WordPress, you can either add JavaScript files via a WordPress theme or plugin.
To link the JS from WordPress theme, you can use:
Similiarly, to link CSS from WordPress theme, you can use:
Detailed usage can be found here.
For WordPress plugins, you can add the following in your main theme file:
As mentioned in the comment above, you shouldn’t include jQuery without careful consideration.
The correct way of adding js files to your WordPress theme is enquing them in functions.php like below
You also don’t need to add your own jQuery since WordPress ships with jQuery already. All you need to do is call it like so:
You can read more about the parameters here: https://developer.wordpress.org/reference/functions/wp_enqueue_script/
You need to enqueue that js file in your theme’s functions.php file.