Steve Taylor photo

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>';
				if ( $i > $displayErrorsLimit ) {
					echo '<li style="padding:2px;border-bottom:2px solid #ccc;"><em>More than '.$displayErrorsLimit.' errors in log...</em></li>';
			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' );


  1. 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.
  2. Paste the above code into your current theme’s functions.php file.
  3. Set the value of the $logfile variable at the start to the absolute server path of your log file.


  • 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…).


  1. 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! :)

  2. Steve Taylor avatar Steve Taylor

    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.

  3. 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.

  4. Steve Taylor avatar Steve Taylor

    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.

  5. Aluminium Bronze Valve avatar Aluminium Bronze Valve

    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.

  6. 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…

  7. Jim avatar Jim

    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!

  8. Ciprian avatar Ciprian

    Still working in 2017!

    Could use a couple of tweaks, though and maybe an exclusion list to exclude certain items.

    Also, an email alert regarding the size and recommendation for issues.

    I might work on it someday.

Comments are closed.