Why does “get_option” return an empty string instead of my default array?

The below code results in the error: Notice: Uninitialized string offset: 0:

$defArray = array( "width" => "", "height" => "" );
$option = get_option( "myoption", $defArray );

//This throws the error
echo $option[ "width" ];

//This shows it as type "string"
var_dump( $option );

According to the docs, I should be able to pass a default value that gets returned if nothing is saved in that option (and nothing is, I just made it up). http://codex.wordpress.org/Function_Reference/get_option

Read More

Why is it not using the default value?

Related posts

Leave a Reply

2 comments

  1. Somehow in refreshing the page, the option got created. I am not sure how as I never submitted the form. Calling delete_option(...) stopped the error from occurring.

  2. you can force the return by parsing args;

    $defArray = array( "width" => "", "height" => "" );
    $option = get_option( "myoption", $defArray );
    $option = wp_parse_args( $option , $defArray  );