For the past few weeks I’ve been working on a WordPress plugin. One of my goals was to have fancy and relevant error messages.
I contemplated writing my own error manager, and even began a very basic one. I experienced hurdle after hurdle, and finally I thought to myself, “Wouldn’t WordPress have its own error manager also?”
So I did a quick source-code search and came across the WP_Error class.
One of the hurdles I ran into in creating my own error manager was error localization. The WP_Error class makes localizing error messages extremely simple.
Adding Error Messages
To add an error message, the first thing you’ll want to do is instantiate your own instance of WP_Error.
$myErrors = new WP_Error();
The next step is to add in your error messages.
$myErrors->add('access_denied', __('You do not have permission to do that.',$myLocalizationName));
There are a few things to notice here. There is something called an error code, which you will use to look up the full error message. You also have the full error message, which uses the __ function for localization.
Retrieving Error Messages
After you have added in your error messages, you’ll want to retrieve them at some point.
Retrieving an error message is as simple as calling the get_error_message method and passing it your error code.
$errorMessage = $myErrors->get_error_message($code);
From there you can echo out your message in whatever manner suits you.
Applications
Using the WP_Error class is ideal for those with themes and plugins.
For plugins, it’s best to have your errors as a member of a class. Using the class approach assures that you can access the errors throughout your methods, and also avoid naming conflicts.
For themes, you can also create your own class, or have a prefixed variable so you don’t have possible conflicts with other variables.
Downloadable and Example Code
Here is some downloadable code with an example of how the class might be used in a theme. As stated earlier, plugin authors may want to use a class for this.
The code is assumed to be placed in a theme’s “functions.php” file.
class my_class {
function my_class() {
$this->localizionName = '';
$this->errors = new WP_Error();
$this->initialize_errors();
}
/* get_error - Returns an error message based on the passed code
Parameters - $code (the error code as a string)
Returns an error message */
function get_error($code = '') {
$errorMessage = $this->errors->get_error_message($code);
if ($errorMessage == null) {
return __("Unknown error.", $this->localizionName);
}
return $errorMessage;
}
/* Initializes all the error messages */
function initialize_errors() {
$this->errors->add('my_weird_error', __('Some weird error has occurred', $myLocalizationName));
$this->errors->add('access_denied', __('You do not have permission to do that.',$myLocalizationName));
} //end function initialize_errors
}
$myErrors = new my_class();
echo $myErrors->get_error('my_weird_error');
The above code has two helper methods, one which retrieves the errors, and one which initializes the errors. The example is very basic, but should give you a good idea on how to use the WP_Error class.
Conclusion
There are many features of the WP_Error class not mentioned here, but you can dissect the code yourself if you like. The class is found in the wp-includes folder under classes.php.
The WP_Error class is a simple and powerful way to store errors, and output them rather easily. And the best thing, the messages can be localized.