View the Debug Log in the Admin
Adds a Tools > Debug Log screen that shows the last lines of wp-content/debug.log, so administrators can check errors without FTP access.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
add_management_page( 'Debug Log', 'Debug Log', 'manage_options', 'wpide-debug-log', function () {
$lines = 200; // Number of lines to show from the end of the file.
$file = WP_CONTENT_DIR . '/debug.log';
if ( defined( 'WP_DEBUG_LOG' ) && is_string( WP_DEBUG_LOG ) && '' !== WP_DEBUG_LOG ) {
$file = WP_DEBUG_LOG;
}
echo '<div class="wrap"><h1>Debug Log</h1>';
if ( ! is_readable( $file ) ) {
echo '<p>No log file found at <code>' . esc_html( $file ) . '</code>.</p></div>';
return;
}
$size = filesize( $file );
$handle = fopen( $file, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions
$chunk = min( $size, 256 * 1024 );
fseek( $handle, -$chunk, SEEK_END );
$content = (string) fread( $handle, $chunk ); // phpcs:ignore WordPress.WP.AlternativeFunctions
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions
$rows = array_slice( explode( "\n", trim( $content ) ), -$lines );
printf( '<p><code>%1$s</code> (%2$s, last modified %3$s)</p>', esc_html( $file ), esc_html( size_format( $size ) ), esc_html( wp_date( 'Y-m-d H:i:s', filemtime( $file ) ) ) );
echo '<pre style="max-height:70vh;overflow:auto;padding:12px;background:#1e1e1e;color:#f0f0f1;font-size:12px;line-height:1.5;white-space:pre-wrap;">' . esc_html( implode( "\n", $rows ) ) . '</pre></div>';
} );
} );
Notes
Turn on WP_DEBUG and WP_DEBUG_LOG in wp-config.php to write the log. Change $lines to show more or fewer lines.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.