8

Is it possible to replace a get_template_directory() in my child's functions.php file?

I want to make changes to the file:

/**
* Load Custom Post types file.
*/
require get_template_directory() . '/inc/post-types.php';

I'd obviously prefer my work not be overwritten when I update my theme, so can I un-register the parent file and then re-register my child's file in my child's functions file?

  • Is there an action in the post-types.php? If so, may be able to remove the action, or maybe a hook in that file? – czerspalace Jun 27 '15 at 7:12
  • post type registration has an action to allow you to modify them, maybe if you explain what you want to change in that file, you won't need to completely replace it. – Milo Jun 27 '15 at 16:21
  • I need to change a lot. Mostly the names and supports of the various custom post types. – Heather Jun 27 '15 at 21:03
  • You can use get_template_directory_uri() in child theme. – Pardeep Oct 4 '19 at 10:43
19

Late answer, but in Wordpress 4.7 two new functions were introduced to address this question.

get_theme_file_path() (for absolute file paths) and get_theme_file_uri() (for URLs) work just like get_template_part() in that they will automatically look in the child theme for that file first, then fallback to the parent theme.

In your example, you could rewrite it using 4.7 to look like this:

/**
* Load Custom Post types file.
*/
require get_theme_file_path( 'inc/post-types.php' );

More information here: https://make.wordpress.org/core/2016/09/09/new-functions-hooks-and-behaviour-for-theme-developers-in-wordpress-4-7/

| improve this answer | |
11

You need to use get_stylesheet_directory_uri() instead of get_template_directory() in your child theme.

From the WordPress codex:

get_template_directory_uri()

In the event that a child theme is being used, the parent theme directory URI will be returned. get_template_directory_uri() should be used for resources that are not intended to be included in/over-ridden by a child theme. Use get_stylesheet_directory_uri() to include resources that are intended to be included in/over-ridden by the child theme.

get_stylesheet_directory_uri()

In the event a child theme is being used, this function will return the child's theme directory URI. Use get_template_directory_uri() to avoid being overridden by a child theme.

| improve this answer | |
  • 2
    Yes, but since get_template_directory() is in my parent theme is there anything I can do from my child theme to change that? – Heather Jun 27 '15 at 15:21

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.