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

SYNTAX

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.

After that add error messages in that class

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:

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:

Conclusion

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

ECMAScript 6 New Features – 2

Posted on 6 years ago

Bhumi