By BishopZ


2019-04-09 22:48:14 8 Comments

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.

2 comments

@Nicolai 2019-04-17 15:29:24

Always check for reserved terms and global variables to avoid possible conflicts.

An overview can be found at the WordPress Codex:



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.

@ocean90 2019-04-12 16:39:21

Since 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.

@BishopZ 2019-04-16 23:56:56

I still haven't gotten the fix to resolve in my instance, but we ran some isolated tests of the issue and you are spot on about the cause and resolution. Bravo

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Debugging an error: wp_enqueue_style was called incorrectly

1 Answered Questions

[SOLVED] Execute upgrade-theme with coding

1 Answered Questions

variable created in page.php is null inside of header.php

2 Answered Questions

[SOLVED] Fatal error in comments.php

2 Answered Questions

[SOLVED] how to test a theme upgrade

0 Answered Questions

Upgrading WP Version + Keeping Customized Core Code?

  • 2013-06-20 20:32:51
  • Edmund
  • 87 View
  • 0 Score
  • 0 Answer
  • Tags:   upgrade

1 Answered Questions

Not able to call class in NHP framework

1 Answered Questions

1 Answered Questions

[SOLVED] Automatic Theme Upgrade hooks?

  • 2012-08-04 20:30:33
  • helgatheviking
  • 75 View
  • 0 Score
  • 1 Answer
  • Tags:   upgrade auto-update

Sponsored Content