What is the method to test for the existence of an option in the database?
Not the value of the option, just that the option exists.
What is the method to test for the existence of an option in the database?
Not the value of the option, just that the option exists.
You must be logged in to post a comment.
Options that don’t exist get the fact of their non-existence cached in memory when you attempt to get them. So you can check that cache to determine the difference.
Alternately,you could pass
NULL
as the default toget_option()
, and then test for it:The reason I suggest using
NULL
as the default rather thanfalse
, is that some options may legitimately return a Boolean value, but should never legitimately returnNULL
.That’s a pretty good question!
Basically you can do several things to get around it, but the most easy is to check if your option is one of the array keys in the “autoloaded” options.
Take a look at
add_option()
s definition: The 4th argument isyes
, which says “Add me to the autoloaded options”.If this is the case, you can simply check the array keys like in the following example: