WordPress Error Handling by Hooks

In the latest WordPress version after (5.0) most of the time we didn’t find actual WordPress error messages many times we have seen simple text messages on your site. “The site is experiencing technical difficulties. Please check your site admin email inbox for instructions.” If you want to get actual error messages we need first to enable debug mode in the wp-config.php file.

If your site is live we can query parameters to open the debugging like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if(isset($_GET['debug'])) {
define ('WP_DEBUG', true);
} else {
define ('WP_DEBUG', false);
}
if(isset($_GET['debug'])) { define ('WP_DEBUG', true); } else { define ('WP_DEBUG', false); }
if(isset($_GET['debug']))		{
  define ('WP_DEBUG', true);
} else {
  define ('WP_DEBUG', false);
}

After this we need to add a filter hook in your theme function file hook name is “wp_php_error_message” for more information you see here https://developer.wordpress.org/reference/classes/wp_fatal_error_handler/display_default_error_template/, https://developer.wordpress.org/reference/hooks/wp_php_error_message/

 

This hook contains 2 parameters

#Parameters

1.) $message


(string) HTML error message to display.

2.) $error


(array) Error information retrieved from error_get_last().

So we can write add hook in your function file like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_filter( 'wp_php_error_message', 'filter_function_for_debug', 3, 2 );
function filter_function_for_debug( $message, $error ){
if(isset($_GET['debug'])) {
//print_r($error);die;
}
return $message;
}
add_filter( 'wp_php_error_message', 'filter_function_for_debug', 3, 2 ); function filter_function_for_debug( $message, $error ){ if(isset($_GET['debug'])) { //print_r($error);die; } return $message; }
add_filter( 'wp_php_error_message',  'filter_function_for_debug', 3, 2 );
function filter_function_for_debug( $message, $error ){
  if(isset($_GET['debug']))		{
    //print_r($error);die;
  }
  return $message;
}

 

Leave a Reply