Check if a Wordpress constant has been defined
Suppose I have configured a constant in the wp-config.php
of my website:
<?php
/\* Super important configuration constant \*/
define('SUPER\_IMPORTANT\_VALUE', '123e4567-e89b-12d3-a456-426614174000');
?>
Whenever you want to use this value in your website, it would be nice to know if the constant has been defined already. You’d expect to be able to do this using something like:
<?php
/\* !empty does not work for a constant \*/
if (!empty(SUPER\_IMPORTANT\_VALUE)) {
// Do stuff
}
?>
But apparently this always resolves to true
.
What does work, is to use php’s defined
function:
<?php
/\* Note the use of quotes, this is important. This example is checking
\* if the string 'TEST' is the name of a constant named TEST \*/
if (defined('SUPER\_IMPORTANT\_VALUE')) {
// Do stuff
}
?>
For more information, check the docs here .