最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Fatal error and use of undefined constant in child theme unless I redefine it

programmeradmin1浏览0评论

Child theme functions.php:

// Init.
require_once( WPBF_CHILD_THEME_DIR . '/inc/init-child.php');

In the parent theme functions.php, the constant is defined:

define( 'WPBF_CHILD_THEME_DIR', get_stylesheet_directory() );

If I include the same constant definition in the child theme like the above, then the require_once works and /inc/init-child.php file is loaded.

However I get a notice stating that the constant is already defined in the parent theme's functions.php (correct!).

But if I remove this from the child theme's functions.php, then I get the following:-

Warning: Use of undefined constant WPBF_CHILD_THEME_DIR - assumed 'WPBF_CHILD_THEME_DIR' (this will throw an Error in a future version of PHP) in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42

Warning: require_once(WPBF_CHILD_THEME_DIR/inc/init-child.php): failed to open stream: No such file or directory in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42

Fatal error: require_once(): Failed opening required 'WPBF_CHILD_THEME_DIR/inc/init-child.php' (include_path='.:/opt/plesk/php/7.3/share/pear') in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42

So why can I only load the file via the child theme if I redefine the constant even though it is going to show me a notice to state it is already defined...?

The same constant is already in use in the very same child theme functions.php file further up as well like:

// Textdomain.
load_child_theme_textdomain( 'page-builder-framework-child', WPBF_CHILD_THEME_DIR . '/languages' );

I am assuming there should be a correct way to load the file needed without having to repeat constant definitions?

Child theme functions.php:

// Init.
require_once( WPBF_CHILD_THEME_DIR . '/inc/init-child.php');

In the parent theme functions.php, the constant is defined:

define( 'WPBF_CHILD_THEME_DIR', get_stylesheet_directory() );

If I include the same constant definition in the child theme like the above, then the require_once works and /inc/init-child.php file is loaded.

However I get a notice stating that the constant is already defined in the parent theme's functions.php (correct!).

But if I remove this from the child theme's functions.php, then I get the following:-

Warning: Use of undefined constant WPBF_CHILD_THEME_DIR - assumed 'WPBF_CHILD_THEME_DIR' (this will throw an Error in a future version of PHP) in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42

Warning: require_once(WPBF_CHILD_THEME_DIR/inc/init-child.php): failed to open stream: No such file or directory in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42

Fatal error: require_once(): Failed opening required 'WPBF_CHILD_THEME_DIR/inc/init-child.php' (include_path='.:/opt/plesk/php/7.3/share/pear') in /var/www/vhosts/xxx/subdomains/xxx/httpdocs/wp-content/themes/page-builder-framework-child/functions.php on line 42

So why can I only load the file via the child theme if I redefine the constant even though it is going to show me a notice to state it is already defined...?

The same constant is already in use in the very same child theme functions.php file further up as well like:

// Textdomain.
load_child_theme_textdomain( 'page-builder-framework-child', WPBF_CHILD_THEME_DIR . '/languages' );

I am assuming there should be a correct way to load the file needed without having to repeat constant definitions?

Share Improve this question asked Aug 18, 2020 at 12:29 zigojackozigojacko 2423 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The important thing that needs to be understood here is that child themes are loaded before the parent parent theme. So if you try to use a function, variable, or constant that's defined in the parent theme in your child theme then it will result in errors, unless you use them inside a hooked function that runs after the parent theme has loaded.

I don't know why your parent theme would be defining the child theme directory itself, you would need to ask its author, but my advice would just be to not rely on its definitions in your child theme. If you want to get path to a file in your child theme, then use get_theme_file_path(), rather than this constant, like this:

require_once get_theme_file_path( 'inc/init-child.php' );

Or this:

load_child_theme_textdomain( 'page-builder-framework-child', get_theme_file_path( 'languages' ) );
发布评论

评论列表(0)

  1. 暂无评论