Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a "view debug log" link in the WP admin bar
<?php
/**
* If we requested the log file, grabit and spit it out.
*
* @return HTML
*/
function rkv_adminbar_display_debug_log() {
// Bail without the query string or not on admin.
if ( ! is_admin() || empty( $_GET['viewlog'] ) ) {
return;
}
// Set my empty.
$build = '';
// Add some basic CSS.
$build .= '<style>
p.returnlink { text-align: center; font-size: 14px; line-height: 22px; }
p.nofile { text-align: center; font-size: 14px; line-height: 22px; font-style: italic; }
p.codeblock { background-color: #fff; color: #000; font-size: 14px; line-height: 22px; padding: 5px 15px; }
p.codeblock br { height: 5px; display: block; margin: 0; padding: 0; }
p strong { font-weight: bold; } p em { font-style: italic; }
code, pre { white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word; }
code pre, span.prewrap { color: #ff0000; }
</style>';
// Include a "back to admin" link.
$build .= '<p class="returnlink"><a href="' . admin_url( '/' ) . '">Back To Admin Dashboard</a></p>';
// Check to make sure we have a file.
if ( ! file_exists( WP_CONTENT_DIR . '/debug.log' ) ) {
$build .= '<p class="nofile">No debug file exists</p>';
} else {
// Get my debug file.
$file = content_url( '/debug.log' );
// Parse out the data.
$data = file_get_contents( $file );
// Trim and break it up.
$data = nl2br( trim( $data ) );
// Convert my line breaks.
$data = str_replace( array( '<pre>', '</pre>' ), array( '<span class="prewrap">', '</span>' ), $data );
// Generate the actual output.
$build .= '<p class="codeblock"><code>' . $data . '</code></p>';
}
// Echo out the build.
echo $build;
// And die.
die();
}
add_action( 'admin_init', 'rkv_adminbar_display_debug_log' );
/**
* Add the link for the debug log file.
*
* @param WP_Admin_Bar $wp_admin_bar The global WP_Admin_Bar object.
*
* @return void.
*/
function rkv_adminbar_link_debug_log( WP_Admin_Bar $wp_admin_bar ) {
// Bail if current user doesnt have cap.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Add the "view debug log" link.
$wp_admin_bar->add_node(
array(
'id' => 'view-debug-log',
'title' => 'Debug Log',
'href' => admin_url( '?viewlog=1' ),
'meta' => array(
'title' => 'Debug Log',
'target' => '_blank',
),
)
);
}
add_action( 'admin_bar_menu', 'rkv_adminbar_link_debug_log', 9999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment