admin管理员组文章数量:1023082
When we upgrade from 5.0.4 to 5.1.1 the site stops loading.
The error message is
Fatal error: Uncaught Error: Call to a member function images_path() on null
/wp-content/themes/mytheme/header.php on line 49
Line 49 is
<?php $theme->images_path(); ?>
above it in the same file is
global $theme;
$theme is created in functions.php as the instance of our custom theme.
class MyTheme {
private $theme_name = "MyTheme";
private $scripts_version = '0.90';
function __construct() {
add_action('init', array($this, 'init_assets'));
...several of these
...more methods
}
}
...other stuff
$theme = new MyTheme();
I don't know how to troubleshoot this issue. Everything worked great prior to the upgrade and no other changes were made to the site.
Any help appreciated.
When we upgrade from 5.0.4 to 5.1.1 the site stops loading.
The error message is
Fatal error: Uncaught Error: Call to a member function images_path() on null
/wp-content/themes/mytheme/header.php on line 49
Line 49 is
<?php $theme->images_path(); ?>
above it in the same file is
global $theme;
$theme is created in functions.php as the instance of our custom theme.
class MyTheme {
private $theme_name = "MyTheme";
private $scripts_version = '0.90';
function __construct() {
add_action('init', array($this, 'init_assets'));
...several of these
...more methods
}
}
...other stuff
$theme = new MyTheme();
I don't know how to troubleshoot this issue. Everything worked great prior to the upgrade and no other changes were made to the site.
Any help appreciated.
Share Improve this question edited Apr 17, 2019 at 15:14 BishopZ asked Apr 9, 2019 at 22:48 BishopZBishopZ 1337 bronze badges 1 |2 Answers
Reset to default 13 +50Since Changeset 44524, which has landed in WordPress 5.1, the variable $theme
is now a global variable set by WordPress which also gets unset after the themes have been bootstrapped:
// Load the functions for the active theme, for both parent and child theme if applicable.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
if ( file_exists( $theme . '/functions.php' ) ) {
include $theme . '/functions.php';
}
}
unset( $theme );
This means that any value set by your theme gets also unset.
To fix the fatal error you now have to replace all variables named $theme
with a prefixed version, for example $my_theme
. Prefixing variables and functions in global scope is considered best practice to avoid such issues.
Always check for reserved terms and global variables to avoid possible conflicts.
An overview can be found at the WordPress Codex:
- Reserved Terms
- Global Variables
Note: Those lists might not be always be 100% up-to-date, so inspecting the version of the source code you're using is the only possibility to be 100% sure.
When we upgrade from 5.0.4 to 5.1.1 the site stops loading.
The error message is
Fatal error: Uncaught Error: Call to a member function images_path() on null
/wp-content/themes/mytheme/header.php on line 49
Line 49 is
<?php $theme->images_path(); ?>
above it in the same file is
global $theme;
$theme is created in functions.php as the instance of our custom theme.
class MyTheme {
private $theme_name = "MyTheme";
private $scripts_version = '0.90';
function __construct() {
add_action('init', array($this, 'init_assets'));
...several of these
...more methods
}
}
...other stuff
$theme = new MyTheme();
I don't know how to troubleshoot this issue. Everything worked great prior to the upgrade and no other changes were made to the site.
Any help appreciated.
When we upgrade from 5.0.4 to 5.1.1 the site stops loading.
The error message is
Fatal error: Uncaught Error: Call to a member function images_path() on null
/wp-content/themes/mytheme/header.php on line 49
Line 49 is
<?php $theme->images_path(); ?>
above it in the same file is
global $theme;
$theme is created in functions.php as the instance of our custom theme.
class MyTheme {
private $theme_name = "MyTheme";
private $scripts_version = '0.90';
function __construct() {
add_action('init', array($this, 'init_assets'));
...several of these
...more methods
}
}
...other stuff
$theme = new MyTheme();
I don't know how to troubleshoot this issue. Everything worked great prior to the upgrade and no other changes were made to the site.
Any help appreciated.
Share Improve this question edited Apr 17, 2019 at 15:14 BishopZ asked Apr 9, 2019 at 22:48 BishopZBishopZ 1337 bronze badges 1-
1
What related code exists before line 49? Variables defined in functions.php are not automatically available in template files. Assuming you renamed $theme to $lp_theme, you should probably be doing a
global $lp_theme;
before trying to use it. – Anastis Commented Apr 12, 2019 at 21:22
2 Answers
Reset to default 13 +50Since Changeset 44524, which has landed in WordPress 5.1, the variable $theme
is now a global variable set by WordPress which also gets unset after the themes have been bootstrapped:
// Load the functions for the active theme, for both parent and child theme if applicable.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
if ( file_exists( $theme . '/functions.php' ) ) {
include $theme . '/functions.php';
}
}
unset( $theme );
This means that any value set by your theme gets also unset.
To fix the fatal error you now have to replace all variables named $theme
with a prefixed version, for example $my_theme
. Prefixing variables and functions in global scope is considered best practice to avoid such issues.
Always check for reserved terms and global variables to avoid possible conflicts.
An overview can be found at the WordPress Codex:
- Reserved Terms
- Global Variables
Note: Those lists might not be always be 100% up-to-date, so inspecting the version of the source code you're using is the only possibility to be 100% sure.
本文标签: Upgrade from 504 to 511 causes theme to be null
版权声明:本文标题:Upgrade from 5.0.4 to 5.1.1 causes $theme to be null 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745579120a2157203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
global $lp_theme;
before trying to use it. – Anastis Commented Apr 12, 2019 at 21:22