Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created June 23, 2011 15:52
Show Gist options
  • Save franz-josef-kaiser/1042827 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/1042827 to your computer and use it in GitHub Desktop.
WordPress debug plugin
<?php
/*
Plugin Name Easy Debug function
Plugin URI https://gist.github.com/1042827
Description Allows the use of <code>_dump( $example_var );</code> or <code>_dump( 'example_global' );</code> for debugging variables. Collects everything in a global & prints in the footer (admin & public). If you want to not print, just return, then set <code>$hook</code> to <code>false</code> when calling the function.
Author Franz Josef Kaiser
Author URI https://github.com/franz-josef-kaiser
Version 0.1
Options Version 0.1
License MIT Extended
(c) Copyright 2010-2011 by Franz Josef Kaiser
@example
# dump $example_var into the footer
_debug( $example_var );
@example
# dump the content of the global $example_global into the footer
_debug( 'example_global', 1 );
@example
# dump $example_var and echo it directly (not into the footer)
_debug( $example_var, 0, 0 );
@example
# dump $example_var into the footer and var_export it
_debug( $example_var, 0, 0, 1 );
*/
/**
* Debug cap
* @param (string) minimum required capability to view debug output
*/
if ( ! defined( 'OXO_DEBUG_CAP' ) )
define( 'OXO_DEBUG_CAP', 'manage_options' );
/**
* DebugBox status
* @param (string) minimum required capability to view debug output
*/
if ( ! defined( 'OXO_DEBUG_BOX_STATUS' ) )
define( 'OXO_DEBUG_BOX_STATUS', '' );
/**
* Debug function
*
* @param unknown_type $input | The variable to print
* @param (string) $global | if set to true, outputs the value of the {$input} global - (optional) default: false
* @param (boolean) $hook | hook or return - (optional) default: true
* @param (boolean) $var_export | true to get var_export, false to just print_r - (optional) default: false
* @return unknown_type $_dump
*/
function _dump( $input, $global = false, $hook = true, $var_export = false )
{
global $_dump;
if ( ! isset( $_dump ) )
$_dump = '';
$global = (bool) $global;
$hook = (bool) $hook;
$var_export = (bool) $var_export;
$backtrace = debug_backtrace();
$counter = count ( $_dump );
$output = '<div class="meta-box-sortables">';
$status = OXO_DEBUG_BOX_STATUS === 'closed' ? ' closed' : '';
$output .= '<div id="debug-'.$counter.'" class="postbox metabox-holder'.$status.'" style="margin: 10px; width: 98%;">';
$output .= '<div title="Click to toggle" class="handlediv"><br /></div>';
$output .= '<h3 class="hndle title widget-top">Debug ';
$output .= '<small><em>(File) </em>';
$output .= isset( $backtrace[0]['file'] ) ? $backtrace[0]['file'] : ' n/a ';
$output .= '<em> (Class) </em>';
$output .= isset( $backtrace[1]['class'] ) ? $backtrace[1]['class'] : ' n/a ';
$output .= '<em> - (Function) </em>';
$output .= isset( $backtrace[1]['function'] ) ? $backtrace[1]['function'] : ' n/a ';
$output .= '<em> - (Line) </em>';
$output .= isset( $backtrace[0]['line'] ) ? $backtrace[0]['line'] : ' n/a ';
$output .= '</small></h3>';
$output .= '<div class="dump inside" style="margin: 10px;"><pre>';
if ( $var_export )
{
$input = var_export( $input );
}
$export = '';
// standard input
if ( ! $global )
$export = print_r( $input, true );
// globals
if ( $global )
{
if( isset( $GLOBALS[ $input ] ) )
$export = print_r( $GLOBALS[ $input ], true );
}
if ( empty( $export ) )
$output .= '<em> - empty - </em>';
$output .= clean_pre( $export );
$output .= '</pre></div>';
$output .= '</div></div>';
if ( ! $hook )
return $output;
return $_dump .= $output;
}
/**
* Add dump to admin & public footer
* Only visible for users with the 'manage_options' capability
* @return void
*/
function _do_dump()
{
if ( ! current_user_can( OXO_DEBUG_CAP ) )
return;
global $_dump;
if ( isset( $_dump ) )
echo $_dump;
}
add_action( 'admin_footer', '_do_dump' );
add_action( 'wp_footer', '_do_dump' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment