While converting a heavily customized premium theme into a proper WordPress child theme I came across a gotcha in WordPress.
If you look at the WordPress Codex information on adding scripts to your theme all of the code samples tell you to find the script by using get_bloginfo('template_directory');
Unfortunately if you’re dealing with a child theme this returns the location of the parent them not the location of the child theme. That means that your file is returning a 404 (it’s not being found).
Again, unfortunately the WordPress Child theme information does not include a remedy for this so after a bit of typing (calling print_r on variations of bloginfo();
) I found that bloginfo('stylesheet_url');
returns the location of your child theme properly.
So the code to include your custom JS script would look like:
[php]
wp_register_script(‘custom_script’, get_bloginfo(‘stylesheet_url’).’/path/to/custom.js’, array(‘name_of_script_dependencies’), ‘1.0’);
[/php]
One response to “Adding Scripts – WordPress Child Theme Gotcha”
I wish you had written this a week ago. 🙂 I was working on admin panels for a child theme and ran into a problem with template_directory. I don’t remember where I located the solution but it was buried in the comments of another article – then I learned to use stylesheet. Took awhile though.