“Warning: Undefined array key” in WordPress: what it means and how to fix it
Short answer
It’s a warning, not an error: a plugin or theme reads an entry of a list that doesn’t exist, such as a setting that was never saved. The page keeps loading. PHP 7 showed the same problem as a quieter notice (“Undefined index”); PHP 8.0 raised it to a warning, which is why it appears after an upgrade. It depends on what is saved on your site, so no code check can predict it. Update the plugin, or send the message to its developer.
The message, exactly
A banner plugin reads its colour setting, but no colour has been saved yet. On PHP 7.4 that gives a notice; on PHP 8.0 the same line gives a warning:
wp-content/plugins/simple-banner/simple-banner.php
<?php
$settings = ['text' => 'Summer sale']; // no 'color' saved yet
echo 'Banner: ', $settings['text'], "\n";
echo 'Color: ', $settings['color'], "\n";
Banner: Summer sale
Color:
Notice: Undefined index: color in /var/www/html/wp-content/plugins/simple-banner/simple-banner.php on line 4Banner: Summer sale
Color:
Warning: Undefined array key "color" in /var/www/html/wp-content/plugins/simple-banner/simple-banner.php on line 4In both cases the page goes on and the colour is simply empty. php.net lists the change for PHP 8.0: “A number of notices have been converted into warnings”, among them “Attempting to read an undefined array key.” (php.net)
Read the message
- The name in quotes (
"color") is the entry the code looked for. Common ones in WordPress logs are setting names,HTTP_REFERERorHTTP_HOST(details of the visitor’s request, which aren’t always sent), and0(the first item of an empty list). - The path names the plugin or theme: the folder after
wp-content/plugins/orwp-content/themes/, here simple-banner. - The line is for whoever fixes the code.
Is it urgent?
Not as an error: nothing stops. It matters in two ways, though:
- Displayed warnings break pages. If your site shows PHP messages, this one lands in the middle of the HTML, or before a redirect, where it can break a form or a login. Keep messages in a log instead: the WordPress debug log shows how.
- Something is missing. The plugin expected a value. The feature may fall back to an odd default, so it’s worth reporting.
Who fixes it
- Find the plugin or theme from the path in the message.
- Check its settings page: saving the settings once can create the missing entry, and then the warning stops.
- Update it (Dashboard → Updates).
- Still there? Send one full line of the message to its developer.
Why no check finds it beforehand
Whether $settings['color'] exists depends on what is saved on your site, not on the code. A check that reads code can’t know it, and CompatNav doesn’t report it: its scan of the examples on this page finds nothing. Your error log is where it shows up. Its sibling, “Attempt to read property … on null”, works the same way for objects.
Key takeaways
- It’s a warning: the page keeps loading, and the missing entry counts as empty.
- PHP 7 called it a notice, “Undefined index: …”; PHP 8.0 made it a warning, “Undefined array key …”.
- The name in quotes is the missing entry; the file and line lead to the plugin or theme.
- It depends on values, such as a setting that was never saved, so it only shows up in your error log.
- The fix for developers: give a default with
??, or check withisset()first.
Frequently asked questions
Is “Undefined array key” dangerous?
Not by itself: the code carries on with an empty value. But the plugin probably expected something there, so the feature may behave oddly, and on pages where warnings are displayed it can break the layout or a form.
Why did it appear after updating PHP?
The same code ran before with a notice, which most sites never logged or displayed. PHP 8.0 turned that notice into a warning.
How do I hide it?
Log warnings instead of displaying them, as shown in our guide to the WordPress debug log. Hiding it doesn’t fix the plugin; updating it does.
What does “Undefined array key 0” or “HTTP_REFERER” mean?
The text in quotes is the entry that was missing: 0 is the first item of an empty list; HTTP_REFERER is the address of the previous page, which browsers don’t always send. The fix is the same: the code should check before reading.
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
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.