Back to Top

How to Use WP_Error class WordPress Error Handling?

How to Use WP_Error class WordPress Error Handling

WordPress comes with an inbuilt WP_Error class so to handle the errors and is easier for developers and WordPress itself. This article will explain in detail how to use WP_Error class WordPress error handling in WordPress. let the novice WordPress developers be a master in this skill.

You can’t judge the user’s behavior on your site and how they use your site is totally unpredictable. So, It is better you develop a website correctly which reject any invalid data input entered by the end user and notifies the user that the entered data is invalid and the process of doing this is known as error handling.

For example, in an email form field, instead of entering a valid email, the user might enter a non-email text. If the website lacks a solid error handling mechanism, the user can gain unauthorized access to sensitive information.

WordPress provide WP_Error class for checking WordPress errors and error messages since version 2.1.0. WordPress uses object of WP_Error class for reporting errors with the several WordPress functions. However, You can use WP_Error class object in plugin or theme to handle error within WordPress. This class containing a various useful method for managing error.

The WP_Error class Constructor has three parameters: $code, $message and $data


$error = new WP_Error( $code, $message, $data );

All the parameters of WP_Error are optional. Whereas $code contain the error code which may be string or integer. $message is the error message and $data is for the error data.

How to initiate class?

First of all, instantiate an instance of WP_Error class to add an error message.You can use the following syntax to create an instance of the class.

$errors = new WP_Error();

After that add error messages in that class

$myErrors->add('my_error_code', __('You do not have permission.',$localizationName));

To handle errors occur in the WordPress core, you can create your own class objects using WP_Error() function in your themes and plugins.

How to get error message in WordPress?

The method get_error_message() method is used to get an error message. These messages often write the most understandable language, as well as carried out through a WordPress localization function, so that messages can be easily displayed on the screen of the user:

if (is_wp_error($result)) {
    wp_die($result-> get_error_message());

First function verified by a is_wp_error() function and returns the created instance of WP_Error. If there are multiple numbers of errors then get_error_messages() returns an array of error messages.

How to get_error_data () method?

Once an error followed with additional information using parameters, you can use the get_error_data() method which is the same as get_error_message() method which accept additional argument as an error code:
Here is the syntax of get_error_data() method:

if (is_wp_error ($result)) {
    $data = $result-> get_error_data ($result-> get_error_code ());


You can use the class WP_Error and its functions is_wp_error() which allow to create, transmit and process the error in WordPress. It is much easier than working with strings, boolean values or global variables for transmission errors, especially when it comes to complex applications by WordPress.

If you have questions or suggestions, please let me know in the comments!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use Ajax in WordPress

Posted on 6 years ago


How to Use Improved MySQLi in PHP

Posted on 7 years ago


How to use Short Hand Array in PHP

Posted on 4 years ago


How to create Custom regions in Drupal

Posted on 6 years ago


Hello world

Updated 1 week ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago