[wp-hackers] Error handling in WordPress

Jeremy Clarke jer at simianuprising.com
Sat Feb 13 17:58:21 UTC 2010

On Sat, Feb 13, 2010 at 12:11 PM, John Eckman <eckman.john at gmail.com> wrote:
> Example:
> - a plugin is adding some function to be called when a new post is published (notifying a third party service for example)
> - sometimes, when that function is called, it returns a failure code
> I'm using try/catch to capture what the exception is that is thrown, but how do I make it visible to the admin user in WordPress?
> Does one set a message to be displayed in the admin dashboard? How?

I'm actually writing a very related patch for core today (feature
freeze is tomorrow, always the procrastinator!)


The goal of my patch is to add error reporting abilities to the
Settings API (i.e. the set of functions that allows you to have
settings pages automatically generated with sections and fields for
particular values that go into a specific get_option() setting) but it
would be great if we made sure your use-case couldn't be solved at the
same time, because they seem related.

You can read more about it at the link above, but the way it will work
is by allowing plugin authors to use add_settings_error() during
setting validation (which happens inside a callback function you chose
when calling register_setting() ), then, when forwarding back to the
settings page from options.php where the processing happens, we add a
query var that tells the next page there are settings errors so that
it can show them at the top of the page. I'm using transients
(get_transient()) to save the error information across pageloads.

One thing I'm not sure about is whether the WP_Error class should be
used as part of this system. I'm not very familiar with it so input is
welcome is somebody else is.

The biggest complicating factor here seems to be the tendency to do
all processing of info (saved settings, saved posts) in a seperate
file (post.php, options.php) then forward back to the user, which
limits error reporting ability greatly. Some kind of system that made
this easier globally would be great, though my goal so far has been to
specifically fix the hole in the Settings API rather than championing
a huge rearchitecting of errors across all of admin. In my case the
automated display of errors will only happen in the particular
circumstance where you have just submitted a settings page. Maybe
adding custom error reporting to the posts editor will require a
similar custom patch to add a query variable just before sending the
user back to the editor page that says 'there are errors to show,
check the transient'. This is necessary because otherwise the errors
will just sit in the transient and could be seen by someone other than
the original submitter.

John, if nothing else you can save your errors to a transient, then in
the admin_notices hook check that transient and show the content (and
delete the transient) when the page reloads. It seems to me that your
use-case should be supported somehow by core though. I know I have
many messages I want to show my users after they save a post and I
can't think of an easy way to do it. If anyone can think of a way to
merge my patch with John's use-case please let me know and I'll try to
include it.

Jeremy Clarke | http://jeremyclarke.org
Code and Design | http://globalvoicesonline.org

More information about the wp-hackers mailing list