-
-
Save yunusga/33cf0ba9e311e12df4046722e93d4123 to your computer and use it in GitHub Desktop.
/* Produces a dump on the state of WordPress when a not found error occurs */ | |
/* useful when debugging permalink issues, rewrite rule trouble, place inside functions.php */ | |
ini_set( 'error_reporting', -1 ); | |
ini_set( 'display_errors', 'On' ); | |
echo '<pre>'; | |
add_action( 'parse_request', 'debug_404_rewrite_dump' ); | |
function debug_404_rewrite_dump( &$wp ) { | |
global $wp_rewrite; | |
echo '<h2>rewrite rules</h2>'; | |
echo var_export( $wp_rewrite->wp_rewrite_rules(), true ); | |
echo '<h2>permalink structure</h2>'; | |
echo var_export( $wp_rewrite->permalink_structure, true ); | |
echo '<h2>page permastruct</h2>'; | |
echo var_export( $wp_rewrite->get_page_permastruct(), true ); | |
echo '<h2>matched rule and query</h2>'; | |
echo var_export( $wp->matched_rule, true ); | |
echo '<h2>matched query</h2>'; | |
echo var_export( $wp->matched_query, true ); | |
echo '<h2>request</h2>'; | |
echo var_export( $wp->request, true ); | |
global $wp_the_query; | |
echo '<h2>the query</h2>'; | |
echo var_export( $wp_the_query, true ); | |
} | |
add_action( 'template_redirect', 'debug_404_template_redirect', 99999 ); | |
function debug_404_template_redirect() { | |
global $wp_filter; | |
echo '<h2>template redirect filters</h2>'; | |
echo var_export( $wp_filter[current_filter()], true ); | |
} | |
add_filter ( 'template_include', 'debug_404_template_dump' ); | |
function debug_404_template_dump( $template ) { | |
echo '<h2>template file selected</h2>'; | |
echo var_export( $template, true ); | |
echo '</pre>'; | |
exit(); | |
} |
saving me 5 years later, thanks for this!
This is awesome. TY!
Thanks!
How do you use this file?
@jl2035 Place the code above into the functions.php
file, then go to a page/post that causes redirect
I'm curious - which part of this deals with errors/404 redirects? At a glance, it seems like it would print things to the screen multiple times per page request.
@nickchomey You are right, most of this is debugging code, however the template_redirect
hook is the redirect part. You could add in there something along the lines of the following to only do this on 404's.
global $wp_query;
if ($wp_query->is_404) {
// do stuff
}
Cool, thanks! That's what I figured. I'll probably move most of that code into the template_redirect function, wrapped in a check like youve provided. Maybe dump the php superglobals as well.
Thank you so much! I was losing my mind and your code helped me figure out the problem.
Very nice gist!! Pro tip: you can make it conditional by checking for, e.g. GET parameter:
Ps. I know we should never touch unsanitized parameters from web request but I assume you would never expose this to public, only local/dev/uat, etc.