Skip to content

Instantly share code, notes, and snippets.

@Mte90

Mte90/php-error-in-wp.php

Last active Apr 14, 2020
Embed
What would you like to do?
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() );
}
@lanterneRouge

This comment has been minimized.

Copy link

@lanterneRouge lanterneRouge commented May 17, 2016

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

This comment has been minimized.

Copy link
Owner Author

@Mte90 Mte90 commented Aug 25, 2016

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

@ghost

This comment has been minimized.

Copy link

@ghost 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
You can’t perform that action at this time.