Are you looking for a way to hide the admin toolbar when an error occurs? This snippet will hide the admin toolbar when a PHP error occurs, letting you see the PHP error text that is normally covered by the toolbar.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function wp_error($errno, $errstr, $errfile, $errline){
$errorType = array (
E_ERROR => 'ERROR',
E_CORE_ERROR => 'CORE ERROR',
E_COMPILE_ERROR => 'COMPILE ERROR',
E_USER_ERROR => 'USER ERROR',
E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR',
E_WARNING => 'WARNING',
E_CORE_WARNING => 'CORE WARNING',
E_COMPILE_WARNING => 'COMPILE WARNING',
E_USER_WARNING => 'USER WARNING',
E_NOTICE => 'NOTICE',
E_USER_NOTICE => 'USER NOTICE',
E_DEPRECATED => 'DEPRECATED',
E_USER_DEPRECATED => 'USER_DEPRECATED',
E_PARSE => 'PARSING ERROR'
);
if (array_key_exists($errno, $errorType)) {
$errname = $errorType[$errno];
} else {
$errname = 'UNKNOWN ERROR';
}
echo $errname. ' Error: [' . $errno . '] ' . $errstr . $errfile . ' on line ' . $errline;
remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
}
if(is_admin()){
set_error_handler('wp_error', E_ERROR ^ E_CORE_ERROR ^ E_COMPILE_ERROR ^ E_USER_ERROR ^ E_RECOVERABLE_ERROR ^ E_WARNING ^ E_CORE_WARNING ^ E_COMPILE_WARNING ^ E_USER_WARNING ^ E_NOTICE ^ E_USER_NOTICE ^ E_DEPRECATED ^ E_USER_DEPRECATED ^ E_PARSE );
}
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: How to Fix the Error Establishing a Database Connection in WordPress (Step by Step).
Comments Leave a Reply