0

I want to include my own javascript code in the front page of a wordpress site. I have located it and have tried many ways online to add javascript code to it but either the console says it failed to load the resource or that the method I called is not defined when I try to use it in an "onload" attribute. I have looked for many articles online but they seem to be talking about how to use it in a specific post. I want this code for the front page only.

This is how I have included the javascript most recently in the header file:

<?php echo '<script type="text/javascript"';
echo 'src="virtue-child/aboutUs.js"></script>'; ?>
3
  • Can you show us how you have included your javascript? Or are you trying to do it through the WP control panel? Commented Dec 29, 2015 at 19:17
  • I tried in different ways to include the code: in the footer file, in the header file, in the actual file. At first I didn't even make it in an external file. Commented Dec 29, 2015 at 21:33
  • console says failed to load, that means it can't find the file. src is probably wrong Commented Dec 30, 2015 at 11:37

1 Answer 1

1

You can create custom script file in js folder of your theme and call in your theme function.php

Ex: your script file name ->customjquery.js

function custom_scripts() {

    wp_enqueue_script( 'customjquery', get_template_directory_uri() . '/js/customjquery.js', array(), '0001', true );

}
add_action( 'wp_head', 'custom_scripts' );
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, I just tried your approach but there are two problems. The first is that this code only gets called if I turn the topbar on in the theme options. The second is that it looks for the file in the parent them and not the child theme even though that is the one that I set.
I have fixed the need for setting the topbar to on by changing wp_head to init in the code but in still gives me this error:
localhost/maths-society/wp-content/themes/virtue/assets/js/… Failed to load resource: the server responded with a status of 404 (Not Found)
Which doesn't make sense because I have the file saved in the folder called virtue-child and that is the current theme.
After making it direct to the child theme I am getting a 403 forbidden error
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.