Monitoring from WP Dashboard

Written By Shane Taubman

August 8, 2018

These changes allow management of PHP error log from the WordPress Dashboard. Using a child theme or parent theme edit functions.php and enter required code for this customisation. Steps required are 

  • PHP logging enabled on server / host
  • PHP.ini created in root of home directory / website
  • php_error.log created in root of home directory / website
  • access to functions.php

PHP.ini is created in root of your home directory for your website. Add the following syntax

Error_Log = /path/to/php_error.log

Code for functions.php.
Copy and paste the following into your functions.php

// PHP Monitoring on Dashboard
function slt_PHPErrorsWidget() {
$logfile = '/home/path-to-root-folder/php_error.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 '
File cleared.
';
if ( $errors ) {
echo ''.count( $errors ).' error'; if ( $errors != 1 ) echo 's'; echo '.'; if ( $userCanClearLog ) echo ' [ CLEAR LOG FILE ]'; echo '
';
echo ''; echo ''; $i = 0; foreach ( $errors as $error ) { echo ''; $errorOutput = preg_replace( '/[([^]]+)]/', '[$1]', $error, 1 ); if ( strlen( $errorOutput ) > $errorLengthLimit ) { echo substr( $errorOutput, 0, $errorLengthLimit ).' […]'; } else { echo $errorOutput; } echo '
'; $i++; if ( $i > $displayErrorsLimit ) { echo 'More than '.$displayErrorsLimit.' errors in log…'; break; } } echo '
';
} else {
echo 'No errors currently logged.
';
}
} else {
echo 'There was a problem reading the error log file.
';
}
}

You may also like…

Google: MyBusiness

Google: MyBusiness

Requirements Google Account created for SEO or business e-mail address. Setup Google My Business...

read more

0 Comments