Advertisement
Creative Coding

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.

Related Posts
  • Code
    Tips
    New wp-config Tweaks You Probably Don't Know8 new wp config tweaks you probably didnt know about 400
    The wp-config.php file: One of the most loved WordPress feature for some, one of the worst nightmares for others. There are countless tips and tricks in a plethora of articles, and you can't get enough of them; however, this article is aiming to be different. In this article, we're going to get familiar with eight new wp-config tricks that are less known than functionality such as turning off post revisions, increasing the memory limit, or other similar features.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Releasing Your ThemeCreating wordpress theme from html 400
    If you've been following this series you now have a working WordPress theme. Your theme has a number of template files, including a page template and an archive template, and also has featured image support.Read More…
  • Code
    Theme Development
    Custom Controls in the Theme CustomizerTheme customizer custom control 400
    In the last article, we explored the advanced controls available in the Theme Customizer, and how to implement them. We’re going to look at how to create our own custom control, allowing you to choose which Category of Posts are displayed on the home page. To get started, download version 0.6.0 of our Theme Customizer Example.Read More…
  • Code
    Plugins
    Using HighCharts in WP-AdminHighcharts 400
    Charts are a great way to present data. They make data more digestible by making it visually appealing. In WordPress, there is no built-in method for getting posts and pages data in a graphical form. Although, there are certain plugins available which integrate Google Analytics with WordPress, but they are overkill if you want to get only a portion of that data. Also, nothing should keep you from learning new techniques and to dive straight into the subject is the best way to learn.Read More…
  • Code
    Plugins
    A Better Forum List Widget for bbPressBbpress
    When bbPress was still a standalone installation, I had tried it out and wasn't really impressed. Things were clunky and it didn't always work the way it was supposed to. After languishing for a few years, Automattic decided to take bbpress and turn it into a plugin, improving the functionality leaps and bounds and making it a strong contender amongst other forum option for WordPress.Read More…
  • Code
    Theme Development
    How to Customize Theme Check for Validating WordPress ThemesTheme check
    Security and consistency are some of the major concerns we have in using third party libraries, frameworks and plugins. We face the same problem in using free and commercial WordPress themes and plugins. The WordPress Codex provides a set of guidelines and rules for developing themes. In order to submit themes to the WordPress theme directory, you have to adhere to those guidelines. But there are hundreds of free and commercial themes which might not be developed according to these guidelines.Read More…