Skip to content

Instantly share code, notes, and snippets.

@Mte90
Last active April 14, 2020 06:09
Show Gist options
  • 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() );
}
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