Readable Variable Dump for Administrators
Adds a wpide_dump() helper that shows any variable in a readable, collapsible box only to administrators, so debug output never reaches visitors even if you forget to remove it.
Opens WPIDE on
Code
if ( ! function_exists( 'wpide_dump' ) ) {
function wpide_dump( $value, $stop = false ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 );
$where = isset( $trace[0]['file'] ) ? str_replace( ABSPATH, '', $trace[0]['file'] ) . ':' . $trace[0]['line'] : '';
printf(
'<details open style="margin:1rem 0;padding:.75rem 1rem;background:#1d2327;color:#f0f0f1;border-radius:4px;font:12px/1.5 monospace;text-align:left;position:relative;z-index:99999"><summary>%s</summary><pre style="white-space:pre-wrap;margin:.5rem 0 0;color:inherit;background:none">%s</pre></details>',
esc_html( 'Dump ' . $where ),
esc_html( print_r( $value, true ) )
);
if ( $stop ) {
exit;
}
}
}
Notes
Example wpide_dump( get_queried_object() ); pass true as the second argument to stop the page after the dump.
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.