0

According to the documentation (https://codex.wordpress.org/Child_Themes and https://developer.wordpress.org/reference/functions/get_template_directory_uri/), it's my understanding that get_template_directory_uri() will return the URL of the parent theme's directory if used within a child theme. But that is not happening for me.

In a child theme I'm trying to develop, when I use get_template_directory_uri() it returns the URL for the child theme's directory, not it's parent theme directory. If I click on Theme Details for the child theme on the manage themes page, WordPress states that the theme is a child of another theme, exactly as I would expect. I have a

Template: parent

line in the child theme's style.css.

Other relevant facts:

  • I'm using WordPress Multi-Site
  • I developed the parent theme and I'm using it for one of the other sites within my Multi-Site and it works just fine
  • Both themes (parent and child) are Network activated and accessible in the site I'm working in
  • I tried pointing the child theme at the twentyfourteen theme and I'm seeing the same behavior, so I don't think it's an issue with the parent theme.
  • I disabled all of my plugins to see if one of them was the issue with no success - none of the plugins appear to be the cause of this
  • I created a copy of twentyfourteen and made it a child theme of my parent theme and same problem happens
  • In my copy of twentyfourteen, I then also added a Template: line in style.css to make it a child them of twentyfourteen and I'm seeing the same behavior as well (i.e., the manage themes page states that the copied theme is a child of twentyfourteen but the get_template_directory_uri function isn't giving me the parent theme's directory)

Does anyone have any suggestions on what could be the issue here? Or am I misinterpreting how get_template_directory_uri() is supposed to work?

  • what is your actual Template: parent line? – hwl May 16 '17 at 23:21
  • If you use the child/parent theme combination on a single site installation does the get_template_directory_uri() function work correctly? – Nathan Johnson May 17 '17 at 0:19
  • @hwl - when trying to point at the Twenty Fourteen template as parent, the line was "Template: twentyfourteen". And the manage themes page is stating that the child theme was pointing at Twenty Fourteen as the parent theme. – Bill May 17 '17 at 18:30
  • @NathanJohnson - I installed a test WordPress site (no multi-site, no plugins, very vanilla) and get_template_directory_uri() works as expected - it correctly points at the parent theme in a child theme. I've been slowly copying over configuration changes to the test site (i.e., making it multi-site, wp-config.php, .htaccess ...) and I still haven't been able to reproduce the issue on the test site, so I don't know what the issue is yet. I may just have to back up the database on the multisite I'm working on and re-install it. – Bill May 17 '17 at 18:33
1

CAUSE

OK, after digging into the WordPress code and the database, the problem was

get_option('template')

which is called by

wp-includes/theme.php : get_template()

was returning the child theme, rather than the parent theme, but only for a specific site within my multi-site. And looking in the wp_{SITE-ID}_options table for that site, the database record where

option_name = "template"

the record was pointing at the child theme, not the parent theme. I'm not sure why Theme Details on the manage themes page in the Dashboard was stating that my problem child theme was a child theme when the database record wasn't correct.

As to why this database entry got screwed up, I'm not sure, but when I was originally trying to develop the problem child theme, it broke my WordPress Multi-Site (including the Dashboard) and so I temporarily renamed the theme's folder name on the server to get WordPress to fall back to a default theme - maybe this was the cause.

SOLUTION

Switching the theme to a different theme and then back to the child theme fixed this - it causes the relevant database record (i.e., option_name = "template" in wp_{SITE-ID}_options) to get set to the correct value, i.e., the parent theme.

  • You hero. Thank you for sharing your solution on here. I inherited a badly made "child" theme that didn't correctly reference the parent. Your solution was exactly right. – Glitcher Aug 1 '18 at 10:59
0

What you need is get_stylesheet_directory_uri, which works exactly the same like get_template_directory_uri but it will return the child theme directory if you are using a child theme, but it will return the parent theme directory if not.

echo get_stylesheet_directory_uri();
  • Thank you for the answer, but I'm using a child theme and trying to get the parent stylesheet, not the child stylesheet. I'm following the directions on codex.wordpress.org/Child_Themes for enqueueing the parent stylesheet along with the child stylesheet in the child theme, but the get_template_directory_uri() function they use in their example returns the child theme URL, which doesn't help me in getting the URL for the parent stylesheet. – Bill May 16 '17 at 23:31
  • oh, sorry, I misunderstood your question. 1) Is there any URL showing this behavior so we can take a closer look? 2) Just to make sure, I suppose you are using the proper handle when enqueing the parent theme style? – Celso Bessa May 17 '17 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.