I’m using the settings API to create a custom settings page for my plugin.
The setup is by the books. Use
Everything is a success, my setting is saved to the DB and recalled when the settings page is opened.
I am using
sanitize_callbackon the setting. The field is an email field and so I’m checking the field using PHP’s
If the email is invalid, I use
add_settings_error()and send the error, this works fine and I see the error on the settings page. If there is no error, I return the email and it saves to the DB.
The problem is that when there is an error, it returns to the settings page and the field has been blanked, emptied, and the previous value in the database was also blanked.
Let’s say they had a valid email in the DB like “firstname.lastname@example.org”. Then they go to change the email “bademail@” and try to save, it will fail the sanitize and send the error message back, but now the email field will be empty and the previous email in the DB is also emptied.
I’m not sure if this is normal behavior. I would think that if sanitize fails, it would not save any value, and the previous value would still be in the DB. Secondly, the user should still see what they previously typed in the field so they have a chance to fix the error and submit again. If the field is emptied, they have to type it all over again, which could be bad.
How do I accomplish these two things then?
1) If the sanitize fails and I use
add_settings_error(), how do I let the user still see what they had typed so they can try to fix it?
2) If the sanitize fails, how do I stop it from saving empty data to the DB and overwriting the previous good value?
- You must be logged in to reply to this topic.