1

I am overriding a function in a parent theme by placing the below code in my child theme's functions.php file.

The function works, but it is throwing this error:

Warning: Use of undefined constant THEME_HOOK_PREFIX - assumed 'THEME_HOOK_PREFIX' (this will throw an Error in a future version of PHP) in /Users/myuser/Local Sites/storytime/app/public/wp-content/themes/buddyboss-theme-child/functions.php on line 82

This is the code in my child theme functions.php:

//Removing the comments section
if ( ! function_exists( 'bjg_buddyboss_theme_single_template_part_content' ) ) {
  function bjg_buddyboss_theme_single_template_part_content( $post_type ) {
    if ( wp_job_manager_is_post_type() ) :
      get_template_part( 'template-parts/content', 'resume' );
    elseif ( gamipress_is_post_type() ) :
      get_template_part( 'template-parts/content', 'gamipress' );
    else :
      get_template_part( 'template-parts/content', $post_type );
    endif;
  }
  add_action( THEME_HOOK_PREFIX . '_single_template_part_content', 'bjg_buddyboss_theme_single_template_part_content' );
}

function change_buddyboss_theme_single_template_part_content() {
    remove_filter( THEME_HOOK_PREFIX . '_single_template_part_content', 'buddyboss_theme_single_template_part_content' );
    add_filter( THEME_HOOK_PREFIX . '_single_template_part_content', 'bjg_buddyboss_theme_single_template_part_content' );
}
add_action( 'after_setup_theme', 'change_buddyboss_theme_single_template_part_content' );

I know that the error is telling me the constant THEME_HOOK_PREFIX is undefined, but I'm not sure why because I copied (and then slightly modified) the bjg_buddyboss_theme_single_template_part_content function from the parent theme. So THEME_HOOK_PREFIX must be defined in the parent theme somewhere because it doesn't throw this error if I remove this code from the child theme.

This is where the constant is defined in the parent theme. The path to this code in the parent theme is buddyboss-theme/inc/init.php:

/**
 * Setup config/global/constants etc variables
 */
private function _setup_globals() {
    // Get theme path
    $this->_tpl_dir = get_template_directory();

    // Get theme url
    $this->_tpl_url = get_template_directory_uri();

    // Get includes path
    $this->_inc_dir = $this->_tpl_dir . '/inc';

    if ( !defined( 'BUDDYBOSS_DEBUG' ) ) {
        define( 'BUDDYBOSS_DEBUG', false );
    }

    if ( !defined( 'THEME_TEXTDOMAIN' ) ) {
        define( 'THEME_TEXTDOMAIN', $this->lang_domain );
    }

    if ( !defined( 'THEME_HOOK_PREFIX' ) ) {
        define( 'THEME_HOOK_PREFIX', 'buddyboss_theme_' );
    }
}
  • Please add the code from the parent theme about how to define the parent theme this constant, on which hook. The solution is dependent on the hook and his order to run. However you should check the constant in your child theme, like if ( ! defined( 'CONSTANT' ) ) define( CONSTANT, 'value' ); – bueltge Aug 28 at 7:02
  • I have added the function where the constant is defined. Hope that was correct. If not here is the full file: pastebin.com/D8adyUC5. Not sure what you're saying about checking the constant in my child theme? How does that code appear in the function? – Ben Turner Aug 28 at 9:05
3

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.

| improve this answer | |

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.