I have a simple meta box that updates the post custom fields (using update_post_meta()
).
How can I send a error or warning message to the next page after the user publishes/updates the post and doesn’t fill one of the meta box fields (or fills them with invalid data) ?
You can do this by hand, but WP natively does it like this for settings errors:
add_settings_error()
to create message.set_transient('settings_errors', get_settings_errors(), 30);
settings_errors()
inadmin_notices
hook to display (will need to hook for non-settings screens).you can use
admin_notices
hookfirst define the notice function:
The you you metabox save function based on if needed add:
Update
Like I promised here is an example of a of how i add an error message form my metabox
Now when looking for this code i found my old way of doing it using
post_updated_messages
filter hook in about the same way so i’ll add that too:This answer [mirror] from Otto in WP Tavern, actually solves the transient problem by doing what WordPress itself does to overcome the redirect problem. Totally worked for me.
I know this question is old but I find the answers here to not solve the issue.
Extending off the answer from Ana Ban, using Otto’s method, I found this to be the best method to handle errors. This doesn’t require storing the errors in the db.
I included a stripped down version of a Metabox object I use. This allows for me to easily add new error messages and ensuring the correct user sees the error message (using the db, this is not a guarantee).