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_' );
}
}
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_' );
}
}
Share
Improve this question
edited Aug 28, 2020 at 9:00
Cat
asked Aug 28, 2020 at 6:24
CatCat
1652 silver badges10 bronze badges
2
|
1 Answer
Reset to default 3Child 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.
if ( ! defined( 'CONSTANT' ) ) define( CONSTANT, 'value' );
– bueltge Commented Aug 28, 2020 at 7:02