I am using something like this on one of my plugins:

$myOption_def = "myOption Default Value";
$myOption = get_option( 'myOption' ) ? get_option( 'myOption' ) : $myOption_def;

That works fine, but the problem is that I need to be able to set the option to "empty", but when I do that (from a textarea on my plugin's option page), I get the default value instead of an empty string because get_option( 'myOption' ) returns the same if my option value is empty than if it doesn't exists.

So how can I figure out when my option doesn't exist (and then set $myOption to my default value), or when my option value is empty (and then set $myOption to an empty string)?

4 Answers 4


Basically to distinguish between false boolean value and '' empty string you must use more strict comparison operator.

var_dump( '' == false ); // this is 'true', treated like two 'empty()' values

var_dump( '' === false ); // this is 'false', because values are both 'empty()' BUT of different type

But there is more. Since what you want is very typical - get_option() already can provide default value on its own. So your code can be simplified to:

$myOption = get_option( 'myOption', $myOption_def );

Note that this will correctly determine empty string and won't apply default value in that case.


You could check for null like so,

$id = "my_option";

$option_exists = (get_option($id, null) !== null);

if ($option_exists) {
    update_option($id, $value);
} else {
    add_option($id, $value);
function optionExists($option_name) {
    global $wpdb;
    $row = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option_name));
    if (is_object($row)) {
        return true;
    return false;
  • 2
    Please explain what your code does and how it works Commented Apr 26, 2015 at 5:37

Oops, figure out myself:

$myOption = (get_option( 'myOption' )==FALSE) ? get_option( 'myOption' ) : $myOption_def; 
  • 2
    don't do so, do what Rarst suggested instead: $myOption = get_option( 'myOption', $myOption_def );. You don't need to reinvent the wheel. It's already in there, a default value for any option. Just pass it as second parameter.
    – hakre
    Commented Feb 9, 2011 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.