[wp-hackers] Settings API: Showing errors if validation fails

Otto otto at ottodestruct.com
Thu Dec 17 15:05:11 UTC 2009


You could use the admin_notices hook to generate your own error
messages based on whatever you like.

Basically, hook a function to admin_notices. In this function, you
need to check for your error case and generate output based on it.
Then, you can unset $_GET['updated'] to prevent the Settings saved
message from showing up.

Basically something like this:

1. On error detection, save the error somewhere that you can pick it
up later. Like an option, or a transient, or something.

2. On the admin_notices hooked function:

if ($parent_file == 'options-general.php' && $_GET['page'] ==
'my-plugin-page') {
// check the plugin option for errors
// found some errors? If so, then {
<div id="message" class="updated fade"><p><strong>No! You'll overload
the reactor!</strong></p></div>
//   unset $_GET['updated']
// }
} // end function

That should do the trick.

-Otto


More information about the wp-hackers mailing list