WordPress Error Handling with WP_Error Class II

In the first part of this series on handling errors in WordPress with the WP_Error class, we took a look at an introduction of the PHP class, we examined the class properties and methods and their roles and functions complemented by code examples.

In this final part of the series, we will be building a fairly simple contact form plugin to demonstrate how to handle errors in plugin development. The contact-form plugin will have shortcode and template tag support so it can be implemented in posts and pages using the former and in a theme using the latter. 

Contact-Form Plugin Development

The contact form will consist of five form fields - four input elements and a textarea element.

I'm sure you are familiar with all the contact-form field. Note that when an email is sent via the contact-form, the phone number get appended to the message. This is evident in the send_mail function.

Let's get started coding the plugin. First the plugin header:

The plugin will consist of five PHP functions as outlined below.

  1. contact_html_form() will contain the HTML form code of the plugin.
  2. validate_form() handles the plugin error. we will see error handling using WP_Error in action in this function.
  3. send_mail() handles the sending of email.
  4. contact_form_function() brings together and process the above functions
  5. contact_form_shortcode() is the shortcode callback function.

The HTML form code of the plugin contained in contact_html_form() function  is as follows.

Next is the error handling function validate_form().

The following are the constraint to be implemented by the plugin which will be enforced by the error handling function.

Note that:

  • no field should be left empty
  • the name field must contain an alphabetic character
  • the email should be valid
  • the phone number should be numeric

The validate_form() function will accept the form fields as it argument so it can validate the form data for errors. Below is the code for the error-handling function that enforces the above plugin constraint fully commented so you'll easily trace its code


The send_mail function handles the email sending.

Let's take a look at what's going on in the send_mail function.

First, the $form_error object is made global so it can be accessed outside the function scope. A check is made to ensure the $form_error object contains no error message. If true, the contact form input is sanitized.

The $to variable stores the email address the message sent via the contact form will be sent to. On a standard contact-form plugin, the variable should contain the WordPress administrator email retrieve from the database or from the plugin settings.

Take note of how the phone number get concatenated to the message.

Finally, the wp_mail function sends the mail. 

The contact_form_function() function processes the function and also serve as the plugin template tag.


Remember the contact-form plugin is going to have shortcode support. Below is the shortcode call-back function together with the add_shortcode function that registers the shortcode.

Using The Plugin

Use the shortcode [cf_contact_form] to include the contact-form in a post or page.
To include the contact-form in your theme, use the template tag <?php contact_form_function(); ?> .

Summary

In this article, we took a look at how to use the WP_Error class to handle errors in plugins. I also showed us a practical use-case on how to handle errors in plugin using the class. You can find the plugin file attached to this article.

Happy Coding!

Tags:

Comments

Related Articles