“Warning: Undefined variable” in WordPress after PHP 8: what changed and the fix
Short answer
A plugin or theme uses a variable, such as $total or $post_id, before giving it a value. PHP treats it as empty and carries on: the page still works. What changed is the label: PHP 7.4 reported a notice (“Undefined variable: total”), which WordPress doesn’t report while WP_DEBUG is off, and PHP 8 reports a warning (“Undefined variable $total”), which it does. So the same old code suddenly shows up in your log, or on screen. Update the plugin or theme; the developer fix is to set the variable first.
The message, on PHP 7.4 and PHP 8
A cart summary adds up prices into $total, but never sets $total before the loop:
wp-content/themes/old-theme/cart-summary.php
<?php
$items = [12.50, 7.25];
foreach ($items as $price) {
$total += $price; // $total was never set before the loop
}
echo "Total: $total\n";
Notice: Undefined variable: total in /var/www/html/wp-content/themes/old-theme/cart-summary.php on line 4
Total: 19.75Warning: Undefined variable $total in /var/www/html/wp-content/themes/old-theme/cart-summary.php on line 4
Total: 19.75The result is the same on both versions, 19.75: PHP treats the missing variable as empty, which here happens to work. Only the message changed, from a notice to a warning, with the variable now written $total.
Why it appeared after the upgrade
php.net lists it among the PHP 8.0 changes: “A number of notices have been converted into warnings”, starting with “Attempting to read an undefined variable.” (php.net)
WordPress’s own setting decides what gets reported. While WP_DEBUG is off, as it should be on a live site, WordPress reports errors and warnings but leaves out notices and deprecation notices (WordPress 7.1.2 source). So on PHP 7.4 this line was silent; on PHP 8 it’s a warning, and it appears in the log, or on screen if your host displays PHP warnings.
Does it matter?
Usually less than it looks. The page works, and in a sum like the one above the result is even correct. It matters when the empty value ends up somewhere visible: a price shown as empty, a link without its address, a heading with no text. The message gives the file and line; open that page and look.
Two things are worth doing anyway:
- Keep the messages off the page. Displayed warnings look broken to visitors and can break redirects and logins: “Cannot modify header information” shows why.
- Pass it on. Warnings fill up logs, and the variable may hide a real bug.
Who fixes it
- Find the plugin or theme: the folder after
wp-content/plugins/orwp-content/themes/in the message. - Update it (Dashboard → Updates).
- Still there? Send the full message to its developer, with the page where it happens.
- Meanwhile, log instead of display: the WordPress debug log.
Can a check find it before the upgrade?
Whether a variable has a value depends on which path the code took, so CompatNav doesn’t report it: we ran its scanner on the example above, with no finding. On PHP 7.4, turn on WP_DEBUG with WP_DEBUG_LOG to log notices too, and search for “Undefined variable”: each line is a warning-to-be on PHP 8. Finding PHP notices on real visits covers doing it on a live site. The same PHP 8 change turned “Undefined index” into “Undefined array key”.
Key takeaways
- A warning, not a fatal error: PHP uses an empty value and continues.
- PHP 7.4: “Notice: Undefined variable: total”. PHP 8: “Warning: Undefined variable $total”.
- WordPress doesn’t report notices while
WP_DEBUGis off, so the code looked clean on PHP 7.4. - Sometimes the result is still right (an empty total plus a price); sometimes a value is missing on the page.
- The developer fix: give the variable a starting value, like
$total = 0;.
Frequently asked questions
Is my site broken?
No. PHP uses an empty value and continues. Check the page the message mentions: if a number or a text is missing there, that’s the effect.
What does “Undefined variable $post” mean in a theme?
The template uses $post without loading it, for example outside the WordPress loop or in a file included from somewhere $post isn’t available. The theme’s developer fixes it, often with global $post; or get_post().
Can I silence it?
Log PHP messages instead of showing them, as in the WordPress debug log guide. That keeps visitors from seeing it; the plugin still needs its fix.
Check your own site before you upgrade
CompatNav is a free WordPress plugin. It reads the code of your plugins and themes on your own server and tells you, in plain words, what will break and what will only show notices on the PHP version you choose. It never changes your code. It can’t see problems that only appear while code runs with real data, so a quick check of your site after the upgrade still matters.
Sources
- php.net: Backward incompatible changes in PHP 8.0.x
- WordPress 7.1.2 source: wp_debug_mode() (load.php)
About the code examples: each output is the real output of the code shown, run with the official PHP builds, without a php.ini, with all errors reported and displayed. Only the file path was replaced by a neutral server path.