Home / Admin / Admin Debug Log
Duplicate Snippet

Embed Snippet on Your Site

Admin Debug Log

Add debug log to admin menu in easier to read format. Not intended for use on production sites.

Code Preview
php
<?php
// Admin Debug Log / Kiri Vadivelu / 2026
// Ensure debug mode enabled at wp-config
// if ( ! defined( 'WP_DEBUG' ) )        define( 'WP_DEBUG', true );
// if ( ! defined( 'WP_DEBUG_DISPLAY' ) ) define( 'WP_DEBUG_DISPLAY', false );
// if ( ! defined( 'WP_DEBUG_LOG' ) )     define( 'WP_DEBUG_LOG', true );
// Or use WP Debugging plugin By Andy Fragen
// Register debug log to admin menu
add_action( 'admin_menu', function () {
    add_menu_page(
        'Debug Log',
        'Debug Log',
        'manage_options',
        'debug-log-viewer',
        'debug_log_viewer_page',
        'dashicons-code-standards',
        80
    );
});
// Check for permission to view log
function debug_log_viewer_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'Insufficient permissions.' );
    }
    $log_path = defined( 'WP_DEBUG_LOG' ) && is_string( WP_DEBUG_LOG )
        ? WP_DEBUG_LOG
        : WP_CONTENT_DIR . '/debug.log';
    $search = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
    $level  = isset( $_GET['level'] ) ? sanitize_text_field( $_GET['level'] ) : '';
    $entries = [];
    if ( file_exists( $log_path ) ) {
        $lines = file( $log_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
        foreach ( $lines as $line ) {
            if ( $search && stripos( $line, $search ) === false ) continue;
            if ( $level && stripos( $line, $level ) === false ) continue;
            $entries[] = $line;
        }
    }
    // Show last 500 by default, most recent first
    $entries = array_slice( array_reverse( $entries ), 0, 500 );
    ?>
    <style>
        .dlv-wrap { font-family: monospace; font-size: 13px; }
        .dlv-controls { margin-bottom: 1em; display: flex; gap: 8px; flex-wrap: wrap; }
        .dlv-controls input[type="search"] { width: 320px; }
        .dlv-log {
            background: #1e1e1e; color: #d4d4d4;
            padding: 16px; border-radius: 6px;
            overflow-x: auto; white-space: pre-wrap;
            word-break: break-all;
        }
        .dlv-line { display: flex; gap: 12px; }
        .dlv-lineno { color: #6a9955; min-width: 48px; text-align: right; user-select: none; }
        .dlv-error   { color: #f48771; }
        .dlv-warning { color: #dcdcaa; }
        .dlv-notice  { color: #9cdcfe; }
        .dlv-fatal   { color: #ff6b6b; font-weight: bold; }
        .dlv-empty   { text-align: center; color: #888; padding: 2em; }
    </style>
    <div class="wrap dlv-wrap">
        <h1>Debug Log Viewer</h1>
        <p>
            <strong>File:</strong> <?php echo esc_html( $log_path ); ?><br>
            <strong>Size:</strong> <?php echo file_exists( $log_path ) ? size_format( filesize( $log_path ) ) : 'N/A'; ?>
        </p>
        <form method="get" class="dlv-controls">
            <input type="hidden" name="page" value="debug-log-viewer">
            <input type="search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="Search…">
            <select name="level">
                <option value="">All levels</option>
                <option value="PHP Fatal error" <?php selected( $level, 'PHP Fatal error' ); ?>>Fatal</option>
                <option value="PHP Warning" <?php selected( $level, 'PHP Warning' ); ?>>Warning</option>
                <option value="PHP Notice" <?php selected( $level, 'PHP Notice' ); ?>>Notice</option>
                <option value="Deprecated" <?php selected( $level, 'Deprecated' ); ?>>Deprecated</option>
            </select>
            <button type="submit" class="button">Filter</button>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=debug-log-viewer' ) ); ?>" class="button button-secondary">Reset</a>
        </form>
        <?php if ( empty( $entries ) ) : ?>
            <div class="dlv-empty">No log entries found. Ensure <code>WP_DEBUG</code> and <code>WP_DEBUG_LOG</code> are set to <code>true</code>.</div>
        <?php else : ?>
            <div class="dlv-log">
                <?php foreach ( $entries as $i => $line ) :
                    $cls = '';
                    if ( stripos( $line, 'fatal' ) !== false )        $cls = 'dlv-fatal';
                    elseif ( stripos( $line, 'warning' ) !== false )   $cls = 'dlv-warning';
                    elseif ( stripos( $line, 'notice' ) !== false )    $cls = 'dlv-notice';
                    elseif ( stripos( $line, 'deprecated' ) !== false ) $cls = 'dlv-notice';
                    else                                               $cls = 'dlv-error';
                    ?>
                    <div class="dlv-line">
                        <span class="dlv-lineno"><?php echo count( $entries ) - $i; ?></span>
                        <span class="<?php echo esc_attr( $cls ); ?>"><?php echo esc_html( $line ); ?></span>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
    <?php
}   

Comments

Add a Comment