Created
October 9, 2019 16:36
-
-
Save bericp1/12e7ed6b91ff755a3d6e6c6ff7eb333f to your computer and use it in GitHub Desktop.
WP Template and Rewrite Rule Debug – just drop in `mu-plugins/`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adapted from | |
* | |
* @param string $where | |
* | |
* @return bool | |
*/ | |
function car_debug_rewrite_rules() | |
{ | |
// only do this on public side | |
if (is_admin()) { | |
return false; | |
} | |
add_filter( | |
'wp_before_admin_bar_render', | |
function () { | |
global $wp_rewrite, $wp, $template; | |
if (!empty($wp_rewrite->rules)) { ?> | |
<style>h5 { | |
background: #000 !important; | |
color: #fff !important; | |
padding: 1em !important; | |
margin: 1em !important | |
} | |
table { | |
margin: 1em !important | |
} | |
table td { | |
border: 1px solid silver; | |
padding: 5px | |
} | |
tr.matchedrule td { | |
border-color: transparent | |
} | |
tr.matchedrule > td { | |
background: HSLA(52, 96%, 67%, 1) | |
} | |
tr.matchedrule + tr.matchedrule > td { | |
background: HSLA(52, 96%, 67%, .5) | |
} | |
tr.matchedrule table td + td { | |
font-weight: 700 | |
}</style> | |
<h5>Rewrite Rules</h5> | |
<table> | |
<thead> | |
<tr> | |
<td></td> | |
<td>Rule</td> | |
<td>Rewrite</td> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
$i = 1; | |
foreach ($wp_rewrite->rules as $name => $value) { | |
if ($name == $wp->matched_rule) { | |
?> | |
<tr class="matchedrule"> | |
<td><?php echo $i; ?></td> | |
<td><?php echo $name; ?></td> | |
<td><?php echo $value; ?></td> | |
</tr> | |
<tr class="matchedrule"> | |
<td colspan="3"> | |
<table> | |
<tr> | |
<td>Request</td> | |
<td><?php echo $wp->request; ?></td> | |
<tr> | |
<td>Matched Rewrite Query</td> | |
<td title="<?php echo esc_attr(urldecode($wp->matched_query)); ?>"> | |
<?php echo $wp->matched_query; ?> | |
</td> | |
<tr> | |
<td>Loaded template</td> | |
<td><?php echo basename($template); ?></td> | |
</table> | |
</td> | |
</tr> | |
<?php } else { ?> | |
<tr> | |
<td><?php echo $i; ?></td> | |
<td><?php echo $name; ?></td> | |
<td><?php echo $value; ?></td> | |
</tr> | |
<?php } | |
$i++; | |
} ?> | |
</tbody> | |
</table> | |
<?php | |
} | |
} | |
); | |
return true; | |
} | |
// If indicated, register our debug hook that puts a table in the footer. | |
if (defined('DEBUG_REWRITE_RULES') && DEBUG_REWRITE_RULES) { | |
car_debug_rewrite_rules(); | |
} | |
// If indicated, fresh rewrite rules on page load | |
if (defined('FORCE_FLUSH_REWRITE_RULES') && FORCE_FLUSH_REWRITE_RULES) { | |
add_action('init', function () { | |
flush_rewrite_rules(true); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based off of this and cleaned up a bit to use valid HTML and to be controlled via constants in wp-config.php and also to have a constant that turns on page load rewrite rule flushing.