Using WordPress ‘is_wp_error_instance’ PHP action

The is_wp_error_instance WordPress PHP action fires when the is_wp_error() function is called, and its parameter is an instance of WP_Error.

Usage

add_action('is_wp_error_instance', 'your_custom_function', 10, 1);

function your_custom_function($thing) {
  // your custom code here
}

Parameters

  • $thing (WP_Error) – The error object passed to is_wp_error().

More information

See WordPress Developer Resources: is_wp_error_instance

Examples

Log WP_Error instances

Log instances of WP_Error for debugging purposes.

add_action('is_wp_error_instance', 'log_wp_error_instances', 10, 1);

function log_wp_error_instances($thing) {
  error_log(print_r($thing, true));
}

Display a custom error message

Display a custom error message when a WP_Error instance is detected.

add_action('is_wp_error_instance', 'display_custom_error_message', 10, 1);

function display_custom_error_message($thing) {
  echo "<b>Error:</b> " . $thing->get_error_message();
}

Send an email notification on WP_Error

Send an email notification to the site administrator when a WP_Error instance is detected.

add_action('is_wp_error_instance', 'send_email_on_error', 10, 1);

function send_email_on_error($thing) {
  $admin_email = get_option('admin_email');
  $error_message = $thing->get_error_message();
  wp_mail($admin_email, 'Error Detected', $error_message);
}

Store WP_Error instances in the database

Store instances of WP_Error in the database for later analysis.

add_action('is_wp_error_instance', 'store_wp_error_instances', 10, 1);

function store_wp_error_instances($thing) {
  global $wpdb;
  $wpdb->insert('wp_error_log', array('error_message' => $thing->get_error_message()));
}

Add a custom error code

Add a custom error code to instances of WP_Error for easier identification.

add_action('is_wp_error_instance', 'add_custom_error_code', 10, 1);

function add_custom_error_code($thing) {
  $thing->add('custom_error_code', 'This is a custom error code.');
}