Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ian izaguirre
3,220 PointsI need help getting my JavaScript file to work on wordpress ?
Hi, I need help getting my JavaScript file to work on WordPress. I have the code listed here in another thread but have gotten no answers, Just click this link for a look at the code I have so far : http://css-tricks.com/forums/topic/need-help-activating-javascript-file-in-my-wordpress-child-theme/
I would love some help because I have trying this for days with no luck. O and I hope you all had a Merry Christmas :-)
3 Answers
Mike Bronner
16,395 PointsI believe the problem is that you are creating a JS file in your child theme. However, you also stated that the include of the JS file is in the parent theme. This means that it is looking for the file in the parent theme folder, not in your child theme folder.
A better way to include javascript files would be to use actions and hooks, as described in this lesson: http://teamtreehouse.com/library/how-to-build-a-wordpress-theme/preparing-to-code-wordpress-templates/linking-javascript-2
ian izaguirre
3,220 PointsAwesome I will take a look at the video some time tonight and will place a response in this thread when I finish if I have any problems with understanding. Thank you again for going all the way with your help I really appreciate it.
Mike Bronner
16,395 PointsNP ... best of luck. Let us know how it works out. :)
ian izaguirre
3,220 PointsHi Mike, So I looked at the video and was a little confused since its in refrence to including a sliders JS . BUT This is my interpretation please let me know if it is correct. So I have the file called myscript.js in my child theme now I want to "activate it for use" so in my child themes functions php file I put the following :
```// Function to load theme javascript function theme_javascript() {
// Register slider script
wp_register_script('myscript',get_template_directory_uri().'js/jquery.myscript.js',array('jquery'),'1.0');
// Enqueue theme script
wp_enqueue_script('myscript',get_template_directory_uri().'js/theme.js',array('jquery','myscript'),'1.0');
}```
Is this all ? is the correct? Thank you
ian izaguirre
3,220 PointsI got a little further here: http://css-tricks.com/forums/topic/i-need-help-getting-my-javascript-file-to-work-on-wordpress-through-child-theme/#post-160138
But still need help answering the JavaScript question posted , if anyone can help me
ian izaguirre
3,220 Pointsian izaguirre
3,220 PointsHi thank you for such a fantastic response :-) , before I look at the better way, I would love to know if this issue I have would be fixed by including the JS file in the parent theme , and not the child theme? Another question would be is , if this works with the JS file in the parent theme then would a theme update remove it since its location is not in my child theme?
Mike Bronner
16,395 PointsMike Bronner
16,395 PointsYour thinking is exactly right: you can't depend on any of the code in the parent theme -- any update to it will likely break your child theme. The video above shows how to include your file in the child theme, without affecting the parent theme, thus isolating yourself from any dangers of updates to the parent. :)
In general: you never want to touch the parent theme when creating a child theme. Always keep all your changes in the child theme itself, and override functionality or style of the parent theme in your child theme. Using hooks (actions and filters) you can remove functionality from the parent theme directly in your child theme (given that the parent theme is coded properly) and then substitute it with your own.
ian izaguirre
3,220 Pointsian izaguirre
3,220 PointsResponse below