I need to add a script in the site footer.
I’m just referencing the footer.php:
<script src="wp-content/file/script.js"></ script>
At Home functions normally, but when access a page child he does not find because search the directory:
site/page-child/wp-content/file/script.js
.
I saw that I have to use:
 wp_enqueue_script ()
But where should I put this code?
Thank’s
You just need to add a “/” before your url in order to start it from root like :
Indeed at home page it looks for yoursite.com/wp-content but on other pages it searches yoursite.com/current-page/wp-content and obviously it results in 404.
Adding / make it always look for yoursite.com/wp-content
Adding
<script src="wp-content/file/script.js"></ script>
is not a clean way to load your JS files because this is not a relative URL and when you active your child theme probably your theme will not load your JS file, the solution is :You need to add this to your
footer.php
file in your theme directorywp-contentthemesyour_themefooter.php
but the best way is to use WP hooks to include your scripts, first you need to register them using
then simply load them
Don’t try to register and enqueue your scripts directly in your footer.php file, instead create a method in your
functions.php
template file then load your scripts usingNote: To enqueue your scripts to the footer you need to set
$in_footer
parameter to true.Checkout wp_enqueue_script for more information.
Since you seem to use WordPress, you can replace
with: