Skip to content

Instantly share code, notes, and snippets.

@Mte90
Last active April 14, 2020 06:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mte90/c869ed85661d65b8c1b0 to your computer and use it in GitHub Desktop.
Save Mte90/c869ed85661d65b8c1b0 to your computer and use it in GitHub Desktop.
PHP error in WordPress backend wrapped as native notice. Refer/screenshot on https://core.trac.wordpress.org/ticket/35155
<?php
/*
Plugin Name: Error For WP backend
Plugin URI: https://github.com/Mte90/
Description: Wrap the PHP errors in the WP admin backend with the native notice
Author: Mte90
Version: 1.0.0
*/
if(! function_exists( 'wp_error_handler' )) {
set_error_handler( "wp_error_handler" );
function wp_error_handler( $errno, $errstr, $errfile, $errline ) {
if ( !(error_reporting() & $errno) ) {
// This error code is not included in error_reporting
return;
}
$err = '';
// If admin wrap the PHP error
if ( is_admin() ) {
$err = "<div class='message error' style='clear:both'><p>PHP ";
}
// Else wrap the code like the classic errors
if ( $errno === 1024 || $errno === 8 ) {
$err .= '<b>Notice</b>';
} elseif ( $errno === 512 || $errno === 2 ) {
$err .= '<b>Warning</b>';
} elseif ( $errno === 256 || $errno === 9191 ) {
$err .= '<b>Error</b>';
}
$err .= ': ' . $errstr . ' In <b>' . $errfile . '</b> on line <b>' . $errline . '</b>';
if ( is_admin() ) {
$err .= '</p></div>';
}
echo $err;
}
}
// Simulate PHP errors
// Remove that to use
add_action( 'plugins_loaded', 'generate_error_example' );
function generate_error_example() {
get_currentuserinfo();
array_merge( 1, array() );
}
@jon-cedar
Copy link

I discovered that if you wrap this in if(!function_exists('wp_error_handler')) {} it results in the following error:

set_error_handler() expects the argument (wp_error_handler) to be a valid callback

Moving set_error_handler() after the function declaration prevents this problem.

@Mte90
Copy link
Author

Mte90 commented Aug 25, 2016

improved the code, sorry but gist don't have notifications -.-'

Copy link

ghost commented May 17, 2018

what is errno 9191 ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment