Skip to content

Instantly share code, notes, and snippets.

@Jonnyauk
Created February 7, 2015 00:01
Show Gist options
  • Save Jonnyauk/0720b69d73acf55418b6 to your computer and use it in GitHub Desktop.
Save Jonnyauk/0720b69d73acf55418b6 to your computer and use it in GitHub Desktop.
Debug function from Wonderflux
<?php
/**
* Displays input in a nicer way for debugging
* Only displays for top level site admin by default
* Part of the Wonderflux theme framework
*
* @param string $input The content you wish to debug - a variable or function.
* @param string $admin_only Only display to top level site admin not other users. Default=true
* @param bool $role Only display to supplied WordPress role. Default = ''
* @param integer $id Only display to supplied user ID. Default = ''
*/
function wfx_debug( $input='', $admin_only=true, $role=false, $id=false ) {
// Check against top level admin
if ( $admin_only && !is_super_admin() )
return;
// Check against user role
if ( $role && !current_user_can($role) )
return;
// Check against user ID
if ( is_integer($id) && get_current_user_id() != $id )
return;
$o = '<div style="color:#000; padding:5px; overflow:auto; border: 4px solid #ff0a0a; background-color: #fec9c9;" class="wfx_debug_output">';
if ( empty($input) ) {
$o .= '<pre>' . esc_attr__('No data returned or false/empty/null', 'wonderflux') . '</pre>';
} else {
switch ( $input ) {
case 'wp_query':
$input_type = 'WordPress core $wp_query';
break;
case 'wp_posts':
$input_type = 'WordPress core $posts';
break;
case 'wp_queried':
$input_type = 'Currently queried object';
break;
case 'wp_all_taxonomies':
$input_type = 'All taxonomies';
break;
default:
$input_type = gettype($input);
break;
}
if ( $input === 'wp_query' ) {
global $wp_query;
$input = $wp_query;
} elseif ( $input === 'wp_posts' ) {
global $posts;
$input = $posts;
} elseif ( $input === 'wp_queried' ) {
$input = get_queried_object();
} elseif ( $input === 'wp_all_taxonomies' ) {
global $wp_taxonomies;
$input = $wp_taxonomies;
}
$o .= '<pre><strong>' . esc_attr__('Debug output for data type:', 'wonderflux') . '</strong> ' . $input_type . '</pre>';
if (is_array($input) || is_object($input)) {
$o .= '<pre>' . print_r($input,true) . '</pre>';
} else {
$o .= '<p>' . $input . '</p>';
}
}
$o .= '</div>';
echo $o;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment