WordPress has a class called WP_Error that provides a really simple way for developers to track errors while processing data. The class mystified me for a long time, but once I figured it out, it is extremely simple to use, and saves a ton of time when attempting to record errors while processing data of any kind. This video is just a quick introduction to the WP_Error class that will show you the very basics of how to use it.
Note: It’s been brought to my attention that the audio level is extremely low in this video. I’m not sure what happened. Sorry.
As noted in the video, I highly recommend that you read through the Codex page on WP_Error and that take a look at the class in the WordPress source.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // sets up the sample short code
function pw_form_shortcode( $atts, $content = null ) {
ob_start(); ?>
<form id="pw_sample_error_form" method="post">
<input type="text" name="pw_numbers" value="" />
<?php wp_nonce_field( 'pw_error_nonce', 'pw_error_nonce_action' ); ?>
<input type="submit" name="pw_process_numbers" value="<?php _e('Submit'); ?>"/>
</form>
<?php
return apply_filters( 'pw_form_shortcode', ob_get_clean() );
}
add_shortcode( 'pw_error_demo', 'pw_form_shortcode' );
// processes our short code
function pw_process_form_submission() {
if( isset( $_POST['pw_process_numbers'] ) && wp_verify_nonce( $_POST['pw_error_nonce_action'], 'pw_error_nonce' ) ) {
$numbers = is_numeric( $_POST['pw_numbers'] )
? stripslashes( $_POST['pw_numbers'] )
: new WP_Error('numbers_only', __('Please only enter numbers', 'pippin' ) );
if( is_wp_error( $numbers ) )
wp_die( $numbers->get_error_message(), __('Input Error', 'pippin') );
// input verified to be numeric, now do something with it
}
}
add_action( 'init', 'pw_process_form_submission' ); |


Excellent video Pippin, glad you tackled this class.
Two questions:
You showed its usage very clearly, now I’d like to know what advantages there are in using the WP_Error class versus doing your validation the normal way, for example with if statements. For sure its always better to use native WordPress functions than custom PHP where possible, and I can see that storing the errors in variables can be useful as well, apart from the ready made error display interface. Are there any other advantages? Should we always use this class when building plugins, or are there any cases where a simple if statement would be more practical?
I also noticed that you have that ob_start() function call right at the top, is that for caching, and when should we be using that?
Thanks!
I think the main reason to use this is just that there isn’t often a reason not to. This class is included in core and works perfectly, so why not use it? I can, however, tell you at least one instance where I chose to not use it, and that was because it wouldn’t work for me. In Easy Digital Downloads, I created my own error tracking system for logging errors during checkout. WP_Error didn’t work here because I needed the errors to be available even after page reload, which is not the case (by default) with WP_Error.
The ob_start() is only for the short code. The contents of a short code must always be returned, so the output buffer simply gives me a way to drop all of the HTML into a variable.
Got that Pippin, thanks!