[wp-hackers] CPT meta box error messages
stas at nerd.ro
Tue Nov 1 12:04:27 UTC 2011
Pe 10.10.2011 00:09, Andy Charrington-Wilden a scris:
> I am trying to figure out how to display a validation error on the input (or lack of) from a meta box in a custom post type.
> Values from a meta box on my CPT are used to query an external API which sends back it's own error messages. The meta box values are being saved via the save_post action. Obviously I can't just echo out a message div there because of the redirect.
> I'm fine with the post being saved regardless of the meta inputs.
> I have looked in to the WP_error class but couldnt figure it out. I've also had a look at transients but that seems just as bad as storing an option as what if two users on the same site submit a post one after another? It's possible the second user will get the error of the first.
> Should I do a redirect with a "message" $_GET variable?? :-/
> Am I missing something glaringly obvious? There seems to be little support for custom error messages generated from a meta box value on a CPT.
> Thank you!
This is probably unexpected, but you can simply generate an
<div id="message" class="updated fade"><p>ERROR</p></div>
What's unexpected, is the fact that WordPress (using js) will place that
element where it should be in case of notification/error in the editor
screen. And yes, that element can be inside a metabox.
Another suggestion would be to use nonces as temporary notification
storage as a replacement for all the hooks listed above (and definitely
for GET parameters), and delete those next time you hit update button.
Because working with an API is not like working with WordPress, you can
have delays and timeouts! Some notifications might want to be saved
until an editor/user reviews the error manually. Nonces should help you
save time while working with slow APIs and use the new CPT outside the
editor (programmatically, hook your API integration together with
`save_post`) without caring if an error occurs (nonces will take care of
those and will show it next time an editor checks the new posted object).
An example of a (slow) API integration using nonces how I explained
above, can be found here:
Sorry for the long post, I know this might look like an overkill, but
believe me *it's worth it* and I hope it will help somebody!
More information about the wp-hackers