“Uncaught Error: Undefined constant” on PHP 8: why it happens and the fix

By CompatNav · Published · Last reviewed · 3 min read

Short answer

It’s a fatal error from PHP 8.0 and later: the code uses a name PHP doesn’t know as a constant. Very often it’s an array key written without quotes, like $args[title] instead of $args['title'], in an old theme or plugin. PHP 7.4 guessed you meant the text 'title' and warned (“Use of undefined constant title - assumed ’title’”); PHP 8 no longer guesses, and the page stops. Switch PHP back if the site is down, then update the theme or plugin, or send the message to its developer. The fix is to add the quotes, or to define the missing constant.

The message, on PHP 7.4 and PHP 8

An old theme prints a heading from an array, with the key written without quotes:

wp-content/themes/old-theme/header.php

<?php
$args = ['title' => 'Welcome'];
echo '<h1>', $args[title], "</h1>\n"; // the key has no quotes

Output on PHP 7.4.33

<h1>
Warning: Use of undefined constant title - assumed 'title' (this will throw an Error in a future version of PHP) in /var/www/html/wp-content/themes/old-theme/header.php on line 3
Welcome</h1>

Output on PHP 8.0.30the script stopped

<h1>
Fatal error: Uncaught Error: Undefined constant "title" in /var/www/html/wp-content/themes/old-theme/header.php:3
Stack trace:
#0 {main}
  thrown in /var/www/html/wp-content/themes/old-theme/header.php on line 3

PHP 7.4 printed the heading correctly and warned, in so many words, that it would stop working one day. PHP 8.0 stops the page. php.net lists it among the PHP 8.0 changes: “Attempting to access unqualified constants which are undefined. Previously, unqualified constant accesses resulted in a warning and were interpreted as strings.” (php.net)

What it means

Without quotes, title isn’t a text for PHP but the name of a constant, a fixed value defined somewhere with define() or const. When no such constant exists, PHP 7 fell back to using the name itself as text, which is why $args[title] worked by accident. PHP 8 refuses the fallback.

So the name in the message tells you what kind of problem it is:

The name in the messageLikely cause
a lowercase word, like "title" or "id"an array key without quotes
a plugin’s constant, like "MY_PLUGIN_VERSION"the plugin that defines it isn’t loaded, or a typo
"ABSPATH"the file was opened directly, outside WordPress

Fix it, in order

  1. Site down? Switch PHP back or pause the theme or plugin: the critical error guide.
  2. Find the file in the message, and from its path the theme or plugin.
  3. Update it (Dashboard → Updates).
  4. Already up to date? Send the message to its developer. For your own theme or child theme, see the fix below.

Find it before you switch

CompatNav doesn’t check for this: our scan of the example above found nothing. But PHP 7.4 reported it as a warning, and WordPress reports warnings even with WP_DEBUG off (WordPress 7.1.2 source). So your host’s PHP error log may already contain the places your site ran: search it for “Use of undefined constant”. The WordPress debug log helps if you can’t reach the host’s log. Other PHP 8.0 changes that stop old themes: each(), curly-brace offsets and nested ternaries, which CompatNav does find.

Key takeaways

  • PHP 7.4: “Use of undefined constant title - assumed ’title’ (this will throw an Error in a future version of PHP)”.
  • PHP 8.0 and later: “Uncaught Error: Undefined constant “title””, and the page stops.
  • The most common cause in old themes: an array key without quotes, $args[title].
  • Other causes: a constant from a plugin that isn’t active, or a typo in a constant’s name.
  • PHP 7.4 reported it as a warning, so your host’s error log may already list the places your site actually ran.

Frequently asked questions

What if the message names ABSPATH?

ABSPATH is defined by WordPress when it loads. If a plugin file is opened directly, outside WordPress, for example when someone or a bot requests the file’s address, the constant doesn’t exist. Plugins usually guard against that with defined( 'ABSPATH' ). If it happens on your normal pages, contact the plugin’s developer.

Why did the theme work on PHP 7.4?

PHP 7.4 treated the unknown name as text and warned that this would become an error in a future version. PHP 8.0 is that version.

The constant comes from another plugin. What now?

Check that the plugin defining it is active and up to date. Code that uses another plugin’s constant should check defined() first; if it doesn’t, tell its developer.

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.

Get CompatNav on wordpress.org How it works

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.