3

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

3 Answers 3

1

You just need to add a "/" before your url in order to start it from root like :

<script src="/wp-content/file/script.js"></ script>

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

Sign up to request clarification or add additional context in comments.

2 Comments

In this way it accesses localhost and not localhost/project
@Daniel Indeed but I wanted to make him understand why it wasn't working, then it's just adaptation. Considering that he uses wordpress, an even better answer would be to use wordpress custom url
1

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 :

<script src="<?php echo get_stylesheet_directory_uri(); ?>/js/script.js"></script>

You need to add this to your footer.php file in your theme directory wp-content\themes\your_theme\footer.php

but the best way is to use WP hooks to include your scripts, first you need to register them using

wp_register_script( 'my_script_name', get_template_directory_uri(). '/js/script.js' );

then simply load them

wp_enqueue_script( 'my_script_name' );

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 using

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

Note: To enqueue your scripts to the footer you need to set $in_footer parameter to true.

Checkout wp_enqueue_script for more information.

3 Comments

In which file I register wp_register_script? How do I upload my js my pc file to the wordpress?
Copy your JS file in your theme project here : wp-content\themes\your_theme\js Then go to wp-content\themes\your_theme\footer.php and add this : <script src="<?php echo get_stylesheet_directory_uri(); ?>/js/script.js"></script> To use wp_register_script, add it to wp-content\themes\your_theme\functions.php if the file doesn't exist create it and it will be automatically loaded..
Here an example of how you can use this : function load_my_script(){ wp_register_script( 'my_script_name', get_template_directory_uri(). '/js/script.js' ); wp_enqueue_script( 'my_script_name' ); } add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
0

Since you seem to use WordPress, you can replace

    <script src="wp-content/file/script.js"></ script>

with:

    <script src="<?php site_url('/wp-content/file/script.js'); ?>"></script>

1 Comment

Could you please check the source code that is produced after performing this change and report back what the output for this <script> block was?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.