• Posted: 2012-08-07
  • Author: Jason Witt
  • Tags:

Hide Toolbar When There’s an Error

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
<?php

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 );
}

?>

Instructions:

Just put this code in your functions.php file or functionality plugin. When you have a PHP error this code will hide the admin toolbar. Letting you see the error.


Related links:
http://innermindlabs.com/quick-tip-wordpress-hide-the-toolbar-for-errors/
Share this snippet

If you like this snippet, share it with friends!