I have an array of site options.
Then I’ve a function that validates all the data submitted by my users using foreach loop. You won’t know where most of the variables come from, but this is not important in this case:
function settings_validation($input) {
$settings = my_array();
foreach ($settings as $setting) {
//this function leaves the old field's value if there's no input
if($type == 'something' && empty($input[$id])) {
$valid_input[$id] = $option[$id];
}
//this function updates the field if input is not NULL
else if($type == 'something' && !empty($input[$id])) {
$valid_input[$id] = $input[$id];
}
return $valid_input;
};
$input is an array of all form fields generated after submit, if field is empty it’s value is ” “.
Now, here comes the trouble.
When I want to erase one of the fields, it’s impossible – when I leave no data within this field and submit form it sends no data and always fires the first if statement and leaves the field in the state before erasing.
If I delete the first if statement then submit the form it sends new data to the options I’ve edited and erases all the others (as their $inputs are empty).
I’m not sure what kind of checks should I do to tell the difference between an empty $input generated by the function and and empty $input created by user’s action?
The settings_validation() function I’m using is a callback for other function called register_setting(), I’m asking here because not on WPSE it’s rather PHP related issue.
The answer was incrediby simple and pretty tricky:
Thanks for helping!