It looks to me that when saving data to the database via the settings API WordPress sanitizes data by default. By that I mean that if I look at the raw settings options in the database they have (at the very least) been through the wordpress equivalent of htmlentities().
Is there any documentation of the exact sanitization process? I don’t want to repeat any of it in my own validation function, and want to make sure I’m using the data correctly when I call it back…..
UPDATE:
In response to Christopher Davis’s great answer, here is a bit more detail.
I am using register_setting
to register a group of settings. This group is set using the add_settings_field
. The array of all settings is passed (using the register_settings callback) to a single validation method, which just checks that everything looks right (i.e. reg exp checking that an email is an email, an integer is an integer etc). I am doing no sanitisation, or referencing any of the WordPress sanitisation methods. However one option value contains a tag, which when viewed in the database has been converted to HTML entities. I assumed WordPress was doing (at least) this by default for any options stored in the database. Perhaps just by the way it converts an array to a string to store it in the database?
WordPress will not do any data sanitization for you. It does do sanitization/validation of the default options.
You have to pass in the third argument of
register_setting
and either role your own validation callback or or use one of the builtins.If your options is only going to contain a string, you could do something like this, for instance.
You can trace how WP saves an option, if you look at the source for
register_setting
(inwp-admin/includes/plugin.php
):The key bit is the last few lines of the function. If there’s a sanitization callback, WP will add it to the filter
sanitize_option_{$name}
.That filter gets applied in
sanitize_option
(inwp-includes/formatting.php
):As you can see, there are a lot of cases there to handle all the built ins, but no default sanitization.
Sanitize option is called in
update_option
to clean/validate things before they go into the database.That’s kind of long winded. Just wanted to show the process by which you could go through and figure out this sort of thing.
Edit
It should be noted that arrays of options will be serialized (via
maybe_serialize
).