override parent theme configuration in child functions.php

You can’t necessarily replace entire arbitrary files with a child theme.

WordPress will automatically look in the child theme for replacements for the templates in the template hierarchy, as well as a couple of additional files like searchform.php or comments.php, but any other files that the parent theme loads are only replaceable in a child theme if the parent theme author has built them to be. This includes any files included into functions.php or in templates.

For a file to be replaceable by a child theme it must be loaded using supported functions. For example, if a theme (like yours) loads a file like this:

require_once get_template_directory() . '/include/aq_resizer.php';

Then a child theme cannot replace it. This is because the '/include/aq_resizer.php' part of the path is not going through WordPress at all. It’s a normal PHP include that WordPress cannot intercept. Additionally, get_template_directory() will only ever be the directory to the parent theme, not the child theme if one is active.

For it to be possible to replace an entire file in a child theme the parent theme must load it with one of these functions:

require_once get_theme_file_path( '/include/aq_resizer.php' );

Because the file path is passed as an argument to get_theme_file_path(), rather than just being a concatenated string passed directly to PHP, it’s possible for the function to look in the child theme first, which it does.

For templates, if the parent theme uses get_template_part(), like this:

get_template_part( 'partials/content' );

Then the child theme can create partials/content.php to replace it, but if the parent theme uses include partials/content.php, then it’s not possible to replace with a child theme.

get_theme_file_path() is much newer (introduced in 4.7) than get_template_part() (introduced in 3.0), so is much less common, and non-existent in older themes. It’s also not widely known.

In your code the parent theme doesn’t use either of these methods, so it’s just not possible to replace the entire file. This means that you will need to replace individual functions one-by-one. The method for doing this will depend on how the function is used.

If the parent theme hooks the function with add_action()

If the parent theme hooks the function you want to replace with add_action(), then you can replace the function by creating a new version of the function in your child theme (with a different name), unhooking the original function with remove_action(), then hooking your new function with add_action():

remove_action( 'hook_name', 'parent_theme_function_name' );
add_action( 'hook_name', 'child_theme_function_name' );

If the parent theme uses the function in a template file

If the parent theme has a function you want to replace, and that function is used in a template file, then you will need to create a new version of the function in your child theme (with a different name) then replace the template file in your child theme, then in your child theme’s version of the template replace the original function usage with your new function.

If the parent theme function is pluggable

Parent themes are loaded before the child theme. This means that it’s possible for theme developers to make functions replaceable by child themes by wrapping then in a function_exists() check. This means that you can replace the function definition with your own function and it won’t cause a conflict, because the parent theme won’t try to re-define it if you already have.

Your code has an example of this: the moto_setup() function is inside this check:

if ( ! function_exists( 'moto_setup' ) ) :
endif;

This makes the moto_setup() function ‘pluggable’, which means that you can define a moto_setup() function in your child theme and the parent theme will use it instead.

Whether or not other functions in your theme are ‘pluggable’ this way is impossible to say without seeing the code.

Conclusion

  • It’s not necessarily possible to replace whole files in child themes. Outside of a handful of core WordPress templates, the parent theme has to explicitly support files being replaceable.

  • If the theme has not made functions’ files replaceable, there are several options for replacing functions depending on how the parent theme was constructed.

  • Depending on how the parent theme was constructed it is entirely possible that there are parts of it that are impossible to replace with a child theme without replacing huge chunks of the theme that you otherwise don’t want to change. This can reach a point where you’re better off just forking the theme and creating a new one.

Leave a Comment