为什么" get_option"返回一个空字符串而不是我的默认数组?

时间:2014-09-03 19:52:41

标签: php wordpress

以下代码会导致错误: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 );

根据文档,如果在该选项中没有保存任何内容,我应该能够传递一个返回的默认值(没有,我刚刚完成了)。 http://codex.wordpress.org/Function_Reference/get_option

为什么不使用默认值?

2 个答案:

答案 0 :(得分:1)

在刷新页面时,选项已创建。我不知道我从未提交过表格。调用delete_option(...)可以阻止错误发生。

答案 1 :(得分:0)

你可以通过解析args强制返回;

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