当窗体未能通过register\\u设置的sanitize回调中定义的验证时,我试图保留窗体的旧值。但它不这样做,而是保存默认值
register_setting(\'setting-wpse3401\', \'wpse3401_options\', array( \'sanitize_callback\' => [$this, \'wpse3401_sanitize_value\']));
public function wpse3401_sanitize_value( $val )
{
$newval = array();
$type = \'updated\';
$msg = \'Settings is saved!\';
$newval[\'form_field_0\'] = sanitize_text_field($val[\'form_field_0\']);
if( $val[\'form_field_1\'] < 1 ) {
$type = \'error\';
$msg = \'Please enter number greater than 0\'
} else {
$newval[\'form_field_1\'] = $val[\'form_field_1\'];
}
add_settings_error(\'wpse3401_messages\', \'wpse3401_message\', __($msg, \'text-domain\'), $type);
if($type == \'updated\'){
return $newval;
} else{
return null;
}
}