Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Introduction to the WP_Error Class

Posted on August 30, 2012 by Pippin in Intermediate, Tutorials, Video Tutorials, Writing Plugins 3 Comments
Home» Tutorials » Intermediate » Introduction to the WP_Error Class
computer error
Tweet
Love It - 3

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' );
Tweet Follow @pippinsplugins
WP_Error

3 comments on “Introduction to the WP_Error Class”

  1. Jean says:
    September 1, 2012 at 12:38 pm

    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!

    Reply
    • Pippin says:
      September 1, 2012 at 9:47 pm

      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.

    • Jean says:
      September 2, 2012 at 3:58 am

      Got that Pippin, thanks!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

Sorry, no related items found.

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (1)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @jasonbobich hmm, interestinf
    May 19, 2013
  • @jasonbobich I haven&#039;t recently
    May 19, 2013
  • @strickland lol I passed out for two hours, so who knows
    May 19, 2013

Topics

Sugar Event Calendar featured contextual help add_options_page attachments get_user_meta register_setting add_shortcode Tom McFarlin Rémi Corson wp_enqueue_script the_content shortcodes short codes login mail chimp authors Related posts attachment plugin image forms do_action bbpress apply_filters recent posts comments post types short code taxonomies custom post type gallery images Ajax Stripe jquery taxonomy users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins