Unfortunately WordPress does not provide the old value as an additional argument to your sanitize_callback
callable. This is supported in general by the option validator hook that WordPress calls when saving the updated setting…
return apply_filters( "sanitize_option_{$option}", $value, $option, $original_value );
…but in practice it is not actually registered with any additional arguments when the filter is added during register_setting
:
if ( ! empty( $args['sanitize_callback'] ) ) {
add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
}
Because add_filter
doesn’t specify that the callable takes any additional arguments, apply_filter
does not provide $option
or $original_value
to your callback when it is called (I consider this to be a bug, and have submitted a ticket to the developers). This means you cannot use the $original_value
to fall back on when the provided setting is invalid. Instead, I suggest you just grab the current setting’s value within your callback:
if ( 'updated' === $type ) {
return $newval;
} else {
// use previous value
return get_option( 'wpse3401_options' );
}