Skip to content

Instantly share code, notes, and snippets.

@yunusga
Created April 5, 2017 11:55
Show Gist options
  • Save yunusga/33cf0ba9e311e12df4046722e93d4123 to your computer and use it in GitHub Desktop.
Save yunusga/33cf0ba9e311e12df4046722e93d4123 to your computer and use it in GitHub Desktop.
Debug WordPress 404 issues (permalinks, rewrite rules, etc.)
/* 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();
}
@fromcouch
Copy link

saved my life. Thanks a lot!

@uselessjargon
Copy link

Led me to my issue. Many thanks!

@alexkuc
Copy link

alexkuc commented Apr 27, 2022

Very nice gist!! Pro tip: you can make it conditional by checking for, e.g. GET parameter:

if (!isset($_GET['debug'])) return;

<gist code>

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.

@jsvini
Copy link

jsvini commented Aug 1, 2022

saving me 5 years later, thanks for this!

@cdils
Copy link

cdils commented Sep 26, 2022

This is awesome. TY!

@mehmetizmirlioglu
Copy link

Thanks!

@jl2035
Copy link

jl2035 commented Dec 28, 2023

How do you use this file?

@alexkuc
Copy link

alexkuc commented Dec 28, 2023

@jl2035 Place the code above into the functions.php file, then go to a page/post that causes redirect

@nickchomey
Copy link

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.

@Braunson
Copy link

@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
}

@nickchomey
Copy link

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.

@janhuntjens
Copy link

Thank you so much! I was losing my mind and your code helped me figure out the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment