Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.