How to use WP_Error $data argument?

$this->error_data[$code] … the WP_Error object holds $data in an array and $code is the key. The add_data method clearly states:

The error code can only contain one error data. But the $data (mixed) can be an array or an object and carry as many keys/properties as you need. It’s up to your handlers how they interpret it.

Error $data is a bonus for further custom processing of the error. Your own special function can throw a WP_Error and you can add custom data into the $data that can be interpreted by your error own handler.

wp_die(); is the same as die; only that it shows formatted output before doing so. It is expected TO DIE. die; in PHP is not conditional. It’s meant to stop there and wp_die(); is designed to mimic functionality with advanced pre-death output capabilities.

The 'title' in the $data support is just a bonus allowing built-in title support for the WP_Error(). It’s designed so they don’t need to choose which error_code‘s message is the title if multiple are present. They just use the default one’s title attribute. NEVER RELY ON SUCH DEFAULT FUNCTIONALITY! Always use your own title in wp_die().

No errors here, just intended behavior… if I understood your question right.

Leave a Comment