WordPress dashboard widget for PHP errors log
Jeff Starr recently posted on ways to monitor PHP errors. For some reason his method using WordPress’s wp-config.php file didn’t work for me, but I got the .htaccess version working OK.
I thought I’d knock out some code to display the latest errors as a widget on the WordPress dashboard.
As usual, this code is provided “as is”—use as you will, and let me know about any problems, but I can’t really offer support to people who aren’t comfortable with WordPress theme development. If anyone finds the time to wrap this up into a user-friendly plugin, do let me know (likewise if there’s already a plugin that does this better—I’ve not looked properly!).
Here goes, instructions are given after the code.
// PHP errors widget
function slt_PHPErrorsWidget() {
$logfile = '/home/path/logs/php-errors.log'; // Enter the server path to your logs file here
$displayErrorsLimit = 100; // The maximum number of errors to display in the widget
$errorLengthLimit = 300; // The maximum number of characters to display for each error
$fileCleared = false;
$userCanClearLog = current_user_can( 'manage_options' );
// Clear file?
if ( $userCanClearLog && isset( $_GET["slt-php-errors"] ) && $_GET["slt-php-errors"]=="clear" ) {
$handle = fopen( $logfile, "w" );
fclose( $handle );
$fileCleared = true;
}
// Read file
if ( file_exists( $logfile ) ) {
$errors = file( $logfile );
$errors = array_reverse( $errors );
if ( $fileCleared ) echo '<p><em>File cleared.</em></p>';
if ( $errors ) {
echo '<p>'.count( $errors ).' error';
if ( $errors != 1 ) echo 's';
echo '.';
if ( $userCanClearLog ) echo ' [ <b><a href="'.get_bloginfo("url").'/wp-admin/?slt-php-errors=clear" onclick="return confirm(\'Are you sure?\');">CLEAR LOG FILE</a></b> ]';
echo '</p>';
echo '<div id="slt-php-errors" style="height:250px;overflow:scroll;padding:2px;background-color:#faf9f7;border:1px solid #ccc;">';
echo '<ol style="padding:0;margin:0;">';
$i = 0;
foreach ( $errors as $error ) {
echo '<li style="padding:2px 4px 6px;border-bottom:1px solid #ececec;">';
$errorOutput = preg_replace( '/\[([^\]]+)\]/', '<b>[$1]</b>', $error, 1 );
if ( strlen( $errorOutput ) > $errorLengthLimit ) {
echo substr( $errorOutput, 0, $errorLengthLimit ).' [...]';
} else {
echo $errorOutput;
}
echo '</li>';
$i++;
if ( $i > $displayErrorsLimit ) {
echo '<li style="padding:2px;border-bottom:2px solid #ccc;"><em>More than '.$displayErrorsLimit.' errors in log...</em></li>';
break;
}
}
echo '</ol></div>';
} else {
echo '<p>No errors currently logged.</p>';
}
} else {
echo '<p><em>There was a problem reading the error log file.</em></p>';
}
}
// Add widgets
function slt_dashboardWidgets() {
wp_add_dashboard_widget( 'slt-php-errors', 'PHP errors', 'slt_PHPErrorsWidget' );
}
add_action( 'wp_dashboard_setup', 'slt_dashboardWidgets' );
Instructions
- Make sure PHP error logging is set up as per Jeff’s post. I don’t think the above code is bothered which of his methods you use—as long as there’s a log file to read.
- Paste the above code into your current theme’s
functions.phpfile. - Set the value of the
$logfilevariable at the start to the absolute server path of your log file.
Notes
- The
slt_prefix is just what I use in my code to prevent clashes with other theme and plugin code. - The “CLEAR LOG FILE” function is restricted so that only WP admins can do it.
- This code uses the Dashboard Widgets API introduced with WordPress 2.7, so it won’t work with WP versions below that (if anyone is cavalier enough to be running such outdated software…).
Welcome! I build websites - mostly based on the brilliant, free & open 

Aldo (10th October 2009)
Hi Steve,
I found interesting this code and transformed it into a plugin.
Please, contact me in my inbox: I’ll send you the package.
Thank you for sharing! :)
Steve Taylor (10th October 2009)
That’s great, Aldo, but code like this is usually folded into my custom themes, so I don’t really have use for a plugin (hence why I don’t get time to create many plugins…). Maybe post a link to your plugin? Also note that Jeff Starr may be developing a plugin from this, too. Worth keeping an eye on.
Aldo (11th October 2009)
I sent him my zipped file.
Ciao!
Rarst (25th February 2010)
I had recently added this to my blog (used plugin from official repository). It worked great at start (many thanks for the coding it btw), but when log grew to few MB (host has issues currently) it started to hang dashboard and generate out of memory error (64MB limit).
So it could use a tweak to protect from choking on large log files.
Steve Taylor (25th February 2010)
Good point. I regularly check and purge mine, but it’s worth having this sort of safety net. I’ll get onto it when I get a moment—do post code if you manage to hack it yourself in the meantime.
bruno (7th May 2010)
I like that. Good job.
Aluminium Bronze Valve (3rd August 2010)
Many thanks for the piece of code and clear instructions! Thank you Steve for posting such useful information, kudos to a job well done and please do keep on posting.
Paula Dalesio (15th September 2010)
it started to hang dashboard and generate out of memory error (64MB limit).
Steve Taylor (15th September 2010)
Hi Paula, I’ve not seen this before, even with relatively large error files. How many errors are in there? All I can suggest is maybe purge it regularly – I’d love to find time to build in a self-purger or something…
Jim (15th December 2010)
What a fantastic idea for a widget this will be endlessly helpful with site debugging. I can’t believe I haven’t found anything like this anywhere else. Thanks for sharing!