This is my first time using a js file. It controls the hover and display of images in a php menu file. They are both in the root directory. I have not referenced the file in the menu.php file at all as I have only learnt of this now, and yet somehow it works.

Do js files just load automatically all the time?

  • there is a lot of library already used by wordpress core or by themes. may be you are lucky but take a look at that to be sure of what appends. – Kaperto Dec 5 '19 at 14:22

WordPress has no mechanism to load custom js files automatically. If your theme need that js file, then place it inside your theme folder. and link it in your template file. for example,

<script src="<?php echo get_template_directory_uri(); ?>/assets/bootstrap.min.js"></script>

In the above example, "bootstrap.min.js" file is located in my theme's "assets" folder. the function get_template_directory_uri() return the url to your theme folder. In this way you can link your custom js files to your template file.

... and yet somehow it works."

As per my experience, this doesn't mean WordPress loading your js file, as @Kaperto said, Wordpress uses a lot of JavaScript libraries by default. Coincidently your expected js file is one of them.

  • "and yet somehow"... through lack of sleep, i forgot that i actually enqueued the js file in my functions.php!!! LOL...I remember now not knowing exactly what was the correct procedure, and that was all the info I could find at the time. So now I have linked it into the php file. In what type of situation would it be correct to enqueue it in the functions.php file? – aye cee Dec 6 '19 at 6:45
  • 1
    If you want that js file to be loaded in all situations, then it could be a solution to include that in functions.php – Sourav Mukhopadhyay Dec 6 '19 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.