Override get_template_directory() in child theme?

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?

2

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/

Leave a Comment