How to solve “Warning: Use of undefined constant” when overriding a parent theme function in the child theme?

Child themes are loaded before the parent theme. That’s why you’re able to replace functions that are wrapped in function_exists(). When themes use function_exists() they are taking advantage of the fact that child themes are loaded first to let the child theme define a function with the same name without throwing an error.

The reason you’re getting an error is because you’re using this constant in your child theme before the parent theme has loaded and defined the constant. However, your parent theme is using defined() in the same way as function_exists() to allow you to define this constant yourself in your child theme:

if ( !defined( 'THEME_HOOK_PREFIX' ) ) {
    define( 'THEME_HOOK_PREFIX', 'buddyboss_theme_' );
}

So all you need to do is define this in your child theme:

define( 'THEME_HOOK_PREFIX', 'buddyboss_child_theme_' );

Now you can use it in your child theme, and your parent theme will pick up on the new value and use that.

Leave a Comment