0

I tried hard finding another question that may answer my query but failed, so if you point any duplicates that would help.

Here is my situation

I have a parent theme and I am developing a child theme for some functionality that I would like to develop and in the child theme I have called wp_enqueue_script multiple times. The parent theme has its own list of scripts and styles.

The issue is once I have a child theme get_stylesheet_directory points to the child theme and I am trying to find if there is a way to avoid this without modifying the parent theme at all.

Here are two methods I tried

//first method where I tried enqueue
add_action( 'wp_enqueue_scripts', 'custom_child_theme_styles' );

//ensure that the child theme scripts are called after the old theme 
add_action( 'after_setup_theme', 'custom_child_theme_scripts' );

and this is what the parent theme does

add_action( 'wp_enqueue_scripts', 'theme_scripts_styles' );

And when the parent theme calls get_stylesheet_directory() it points to the child theme folder and hence javascripts from the parent theme dont load at all.

3
  • Can you show us where you're using the function and what you're using it for? You don't need to enqueue the parent themes scripts and styles as both parent and child theme functions.php are loaded
    – Tom J Nowell
    Commented Feb 23, 2016 at 14:38
  • My theme uses many javascript files for things like masonry and such, my child theme is a functional theme for a full fledged reporting system which needs tabs, uploaders and other such functions. I am calling the script from within child theme functions.php and the parent theme code that I have demonstrated is within the parent theme. I am only trying to load my scripts, but since the parent theme is anyways called, the get_stylesheet_directory() function inside the parent functions.php is returning the child theme folder
    – skv
    Commented Feb 23, 2016 at 17:38
  • Ah I understand now
    – Tom J Nowell
    Commented Feb 23, 2016 at 19:38

1 Answer 1

5

The problem your facing here can be solved by changing the function being used:

  • get_stylesheet_directory will give you the directory of the current active theme, aka the location of the active style.css
  • get_template_directory will give you the directory of the active theme, aka the parent theme

For most themes the two give the same result, but:

  • Always use get_stylesheet_directory when referring to things in a child theme
  • Always use get_template_directory when in a parent theme
  • Always use get_template_part to load a template, never use the previous 2 function
  • Use include and require to load a file that doesn't contain a template such as a class or PHP include. If you need to override these, use hooks and actions to do this via remove_action etc
  • Minimise any cross talk between child themes and parent themes. A child theme shouldn't be attempting to include files in a parent theme etc other than through get_template_part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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