Adding Scripts – WordPress Child Theme Gotcha

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:

wp_register_script(‘custom_script’, get_bloginfo(‘stylesheet_url’).’/path/to/custom.js’, array(‘name_of_script_dependencies’), ‘1.0’);

1 thought on “Adding Scripts – WordPress Child Theme Gotcha

  1. 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.

Comments are closed.

To get a head start, join thousands of other men and Stop The Struggle.

Get my free eBook to help you stop working all the hours without traction.

You’ll also get a weekly email with good resources to help you do family with the same intention you do your work. Both need to run well if you want to win.

%d bloggers like this: