How would I go about not letting empty data into wordpress?
<?php
foreach($_POST['eirepanel_inline_ads_options_name'] as $post_eirepanel_inline_ads_options_name):
if(empty($post_eirepanel_inline_ads_options_name)):
echo 'empty';
else:
update_option('eirepanel_inline_ads_options', $_POST);
$eirepanel_inline_ads_options = get_option('eirepanel_inline_ads_options');
endif;
endforeach;
?>
More importantly, you should not let *untrusted, unsanitized $_POST data* into WordPress.
But I think the issue is that you’re updating the option with the entire $_POST data, instead of the appropriate array key:
Should probably be something like:
Are your Plugin options discrete (one DB entry per option), or an options array?
EDIT
Since you’re using an options array, the correct approach would be:
$_POST
data ($input = array()
)$valid_input = array()
)$_POST
data$valid_data
array with the sanitized$input
array$valid_data
back to the DBe.g.
Just quick and dirty, but should give you an idea.
Also: using the Settings API would be especially helpful here.
Following on from your response to @Chip’s suggestions, I’d advise you name your inputs like so;
Then in your code;
$options
will now only contain the explicit keys passed toshortcode_atts()
, with the data from$_POST
overriding the defaults.You’ll see I’ve use input array names, so as to namespace your
$_POST
data (as opposed to namespacing every option input name, then having to loop over them and remove it before saving).Note: This only strips out keys we don’t want. You’ll probably want to go further and sanitize the individual values, dependent on their data type. If they’re all the same, you could map a callback to the array and have done with it.
For example, it they’re all plain text fields;