在Chip Bennet的大力帮助下,我终于为Settings API创建了有效的验证回调,只有一个小故障我不知道如何修复。
下面是我的验证函数中的foreach循环:
foreach ($settings as $setting) {
$id = $setting[\'id\'];
$type = $setting[\'type\'];
$option = get_option(\'XX_theme_settings\');
if($type == \'textarea\') {
$valid_input[$id] = wp_filter_nohtml_kses($input[$id]);
}
}
我正在循环使用许多$设置,上面的代码并不像预期的那样工作,如果我的设置中有10个文本区域,并且我只编辑一个文本区域,那么其他9个的$输入[$id]将为空,并且它们将立即被删除。我试图通过其他输入检查来解决这个问题,例如:
//if there is no input for given field leave the old value
if($type == \'textarea\' && empty($input[$id])) {
$valid_input[$id] = wp_filter_nohtml_kses($option[$id]);
}
//if there\'s input sent change setting\'s value
else if($type == "textarea" && !empty($input[$id])) {
$valid_input[$id] = wp_filter_nohtml_kses($input[$id]);
}
它基本上可以工作,但无法擦除选项。例如,如果我有一个名为“welcome\\u text”的选项,并且附加了值“welcome People”,我就不能删除所有符号并将此字段留空,因为第一个if语句正在触发。因此,“welcome\\u text”值可以是“welcome fol”或“W”,但决不能是“”。我可以看出我的第一种方法非常有效in here, 我不知道为什么。
谢谢:)