Advertisement

Display PHP Errors as WordPress Admin Alerts

by

The PHP language is widely adopted for many web applications like WordPress. As powerful as PHP is it does have some drawbacks, but one of the my favorite features of PHP is that it will display errors right on the web page. The new toolbar that was introduced in WordPress 3.3 interferes with this functionality. The toolbar has a fixed absolute position which covers the first few lines of PHP errors.


Options for Displaying Errors

With the new toolbar covering PHP errors we need to use other options for debugging our themes and plugins. Fortunately for us, there are a few options for us to choose from.


Plugins

Since the inception of the new toolbar plugin developers have came up with a few plugins that can help us with this issue. Here are a couple of plugins you can use to disable the toolbar.

All these plugins offer different features, but they all allow us to disable the toolbar. Which will allow us to view any PHP errors that come our way. The nice thing with these plugins is that they can can be enabled and disabled when needed.


Error logging

The solution for PHP errors that is suggested by the WordPress Core Team is to use error logging. If your developing on WordPress you should already know about the WP_DEBUG constant that is defined in the wp-config.php file. You can also define the WP_DEBUG_LOG constant. Here is an example that the WordPress Codex gives for enabling the debug log.

define('WP_DEBUG', true); // false
if (WP_DEBUG) {
  define('WP_DEBUG_LOG', true);
  define('WP_DEBUG_DISPLAY', false);
  @ini_set('display_errors',0);
}

This will output any PHP errors to a file named debug.log in the wp-content folder. Logging errors like this has it ups and downs. When working on a commercial plugin or when working in a team environment where logging errors is necessary. This is the best solution for tracking your errors, but when developing a plugin or theme on on a smaller scale, this just isn't practical. When all you need to do is make sure all your I's are dotted and T's are crossed. It can be very frustrating have to open, close and re-open the error log every time you make a change.


Display Errors as Admin Alerts

The method I've adopted, is to output PHP errors as admin alerts. I think this is the best solution so far. No plugins are required, it's more convenient than error logging, and it can be left in place without interfering with layout and design of the Admin Area or your WordPress themes.

The code used to achieve this is simple, and I can't quite understand why this isn't a built in part of the WordPress Core. The following code should be placed in your functions.php file or in your functionality plugin.

function admin_alert_errors($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';
	}
ob_start();?>
<div class="error">
  <p>
  	<strong><?php echo $errname; ?> Error: [<?php echo $errno; ?>] </strong><?php echo $errstr; ?><strong> <?php echo $errfile; ?></strong> on line <strong><?php echo $errline; ?></strong>
  <p/>
</div>
<?php
echo ob_get_clean();
}

set_error_handler("admin_alert_errors", 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 );

There are four arguments that are passed to the admin_alert_errors() function.

  • $errno : Outputs the level of the error raised. Each error type has a number associated with it. $errno displays that number.
  • $errstr : Outputs the error message.
  • $errfile : Outputs the name of the file that has the error.
  • $errline : Outputs the line that has the error.

The $errorType array defines the title used for each error type.

e.g. WARNING Error: [2] include(wuzup) [function.include]:

These can be anything you want. It can say "Wuzup G, You messed Something Up".

e.g. WARNING Wuzup G, You messed Something Up: [2] include(wuzup) [function.include]:

I don't recommend using that for anything you're making for a client unless they have a really good sense of humor, but you get the idea.

The IF statement checks to see if the the error type matches any of the error constants you defined in the $errorType array. If not, then the error title will be displayed as "UNKNOWN ERROR"

The last part of the function is what will be outputted. The class error is a default class built into WordPress used to style error admin alerts.

To initiate the admin_alert_errors() function you'll use the set_error_handler() function. The first parameter is the function used to display the errors. In this case the admin_alert_errors() function. The next set of parameters are the PHP error constants that you want to display.

If you have any experience in dealing with error handling you'll notice that E_STRICT isn't included. This is because there are some errors that are coming from the WordPress Core. I'm not sure if this is something missed by the Core WordPress Team or if it's by design, but either way it's not necessary to view these errors. Also, using E_ALL instead of listing all the error constants doesn't seem to work. If you use E_ALL not all the errors will be displayed as admin alerts.

note: This doesn't affect the way errors are displayed on the frontend of your site. Only in the Admin pages.

another note: When I have the WordPress test data installed. I receive a DEPRECATED error for the class-simplepie.php file.


Conclusion

Well, I hope this helps you with those pesky PHP errors. If anyone has another solution, please post it in the comments. I'd love to see what you've come up with.

Advertisement