Skip to content

Instantly share code, notes, and snippets.

@broskees
Last active October 13, 2022 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save broskees/18c6b10a491d2946fbbfed96a8dd6dc4 to your computer and use it in GitHub Desktop.
Save broskees/18c6b10a491d2946fbbfed96a8dd6dc4 to your computer and use it in GitHub Desktop.
A nice wp_debug_var() as a mu-plugin to include in your projects
<?php
/*
Plugin Name: WP Debug Function
Description: A Must-Use plugin that makes the wp_debug_var available to your WordPress Installation
Plugin URI: https://gist.github.com/broskees/18c6b10a491d2946fbbfed96a8dd6dc4
Version: 1.0.0
Author: Joseph Roberts
Author URI: https://github.com/broskees
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined( 'ABSPATH' )) {
exit; // Exit if accessed directly
}
class WP_DEBUG_VAR
{
private $vars;
private $wp_backtrace;
private $output;
public function __construct($vars)
{
$this->vars = array_filter(array_map(
function ($var) {
return htmlspecialchars(print_r($var, true));
},
$vars
));
$this->wp_backtrace = print_r(wp_debug_backtrace_summary(null, 2, false), true);
}
public function printOutputAndLive()
{
echo $this->getNotDieOutput();
}
public function printOutputAndDie()
{
wp_die($this->getOutput());
}
private function getOutput($live = false)
{
ob_start();
?>
<style>
.tab_content {
<?php if (!$live): ?>
max-height: calc(100vh - 1em - 170px); /* Make it fix within wp_die message nicely */
<?php else: ?>
max-height: 237px;
<?php endif; ?>
overflow-y: scroll;
display: none;
}
#tab_1:checked ~ #content_1,
#tab_2:checked ~ #content_2,
#tab_3:checked ~ #content_3 { display: block; }
</style>
<div>
<input type="radio" name="tab_check" class="tab_check" id="tab_1" checked>
<label for="tab_1">Var Output</label>
<input type="radio" name="tab_check" class="tab_check" id="tab_2">
<label for="tab_2">WP Backtrace</label>
<input type="radio" name="tab_check" class="tab_check" id="tab_3">
<label for="tab_3">PHP Backtrace</label>
<div class="tab_content" id="content_1">
<?php foreach($this->vars as $var): ?>
<pre><?php echo !empty($var) ? $var : '' ?></pre>
<?php endforeach; ?>
</div>
<div class="tab_content" id="content_2">
<pre><?php echo !empty($this->wp_backtrace) ? $this->wp_backtrace : '' ?></pre>
</div>
<div class="tab_content" id="content_3">
<pre><?php debug_print_backtrace() ?></pre>
</div>
</div>
<?php
return ob_get_clean();
}
private function getNotDieOutput()
{
ob_start();
?>
<style>
.wp_debug_var_modal {
position: fixed;
height: 300px;
width: 100vw;
bottom: 0;
background-color: #fff;
padding: 20px;
z-index: 9999999999;
}
</style>
<div class="wp_debug_var_modal">
<?= $this->getOutput(true) ?>
</div>
<?php
return ob_get_clean();
}
}
/**
* Debugging function
*
* @param mixed $variable
*/
function wp_debug_var(...$vars) {
if (apply_filters('wps_debug_off', false)) {
return;
}
(new WP_DEBUG_VAR($vars))->printOutputAndDie();
}
/**
* Debug and don't die
*
* @param mixed $variable
*/
function wp_debug_var_live(...$vars) {
if (!current_user_can('manage_options')) {
return;
}
add_filter('wp_debug_vars', function ($already_set_vars) use ($vars) {
if (!empty($vars)) {
$already_set_vars = array_merge_recursive($already_set_vars, $vars);
}
return $already_set_vars;
});
$callback = function () {
(new WP_DEBUG_VAR(
apply_filters('wp_debug_vars', [])
))->printOutputAndLive();
};
if (!has_action('shutdown', $callback)) {
add_action('shutdown', $callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment