WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am setting up a plugin and have used the setting API for the first time to create a form in settings. The data for the plugin is initially created by default when the plugin is activated and stored in the database in a serialized format.

Today I added the validation callback function, which works fine to do validation. However when any field fails validation and the function returns false (I have also tried returning NULL) the existing valid data in the database is deleted.

Question is this how things are supposed to happen or am I doing something stupid? I would have expected in this situation that the existing valid data in the database would have remained unaltered until such time as the user had presented a set of valid data to alter it.

Any suggestions appreciated.

share|improve this question
Post your code please. – s_ha_dum Jan 29 '13 at 19:04

Your sanitize_callback function should not return false or null indicating that input is not valid - its intent is to always return a valid value. The value returned by this function is what will be save to the database, so in order for it to actually sanitize anything it will have to return something that makes sense. Compare it with using intval as in this example, which will force the settings API to store an int value no matter what the input.

Anyway, to solve you problem your should:

  • Return the previously stored value if the input is invalid
  • Return a default value if input is empty (and empty values is not permitted)
  • Return the newly input value if it is deemed valid

Also, in addition to the last statement, your value might well become valid during sanitation: imagine inputs such as " user@host.com" or "0123" which might get be sanitized to "user@host.com" and 123 depending on your needs.

share|improve this answer
Simon thanks for your input. I wondered if it would need to work that way but it seemed such a strange way to handle validation processing that it was worth asking the question. – Pete Lawless Jan 30 '13 at 13:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.