Handle all the class loading in your parent theme on a predictable action (point in time) and hook in later in your child theme.
Example:
add_action( 'wp_loaded', 'parent_prefix_load_classes', 10 );
function parent_prefix_load_classes()
{
$classes = [ 'Extra_Comment_Walker', 'Extra_Nav_Menu_Walker' ];
foreach ( $classes as $class )
{
locate_template( "php/class.$class.php", TRUE, TRUE );
}
}
Create instances in your child theme make sure your code runs after the classes are loaded:
// Priority 11 to run after the parent theme's loader.
add_action( 'wp_loaded', 'child_prefix_create_objects', 11 );
function child_prefix_create_objects()
{
$nav_walker = new Extra_Nav_Menu_Walker;
}
Rules of thumb:
- Never load anything just when the file (
function.php
) is called. Wait forwp_loaded
. - Use the priority argument to control the order of execution.
Some notes:
- Custom post types, taxonomies and shortcodes belong to plugins. They should never be part of a theme, because that would create a lock-in effect for the user. If a theme switch would break the content you did something wrong.
- Do not use
require_once
in a theme.locate_template()
is more flexible. You can overwrite the whole class now in your in child theme if you use the same directory structure.