Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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();
}
@marlonlamancio

This comment has been minimized.

Show comment Hide comment
@marlonlamancio

marlonlamancio May 3, 2012

There is a single quote missing in line 19.

There is a single quote missing in line 19.

@soulseekah

This comment has been minimized.

Show comment Hide comment
@soulseekah

soulseekah May 3, 2012

@marlonlamancio, fixed, thanks! :)

Owner

soulseekah commented May 3, 2012

@marlonlamancio, fixed, thanks! :)

@murraybunton

This comment has been minimized.

Show comment Hide comment
@murraybunton

murraybunton Jun 25, 2013

Very useful, thanks!

Very useful, thanks!

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Jul 12, 2013

Was also getting 404 error I found this little nugget in the Centos 6 httpd.conf under the section. Just hit page down like 2 or three times and you will find this. I made a quick article here with more details.

http://www.mobiledataup.com/fix-wordpress-permalink-404-error-with-centos-6/

AllowOverride controls what directives may be placed in .htaccess files.

It can be “All”, “None”, or any combination of the keywords:

Options FileInfo AuthConfig Limit

AllowOverride none

ghost commented Jul 12, 2013

Was also getting 404 error I found this little nugget in the Centos 6 httpd.conf under the section. Just hit page down like 2 or three times and you will find this. I made a quick article here with more details.

http://www.mobiledataup.com/fix-wordpress-permalink-404-error-with-centos-6/

AllowOverride controls what directives may be placed in .htaccess files.

It can be “All”, “None”, or any combination of the keywords:

Options FileInfo AuthConfig Limit

AllowOverride none

@zhengtu

This comment has been minimized.

Show comment Hide comment
@zhengtu

zhengtu Mar 2, 2016

it's really solve my issue, thank you.

zhengtu commented Mar 2, 2016

it's really solve my issue, thank you.

@frogFangio

This comment has been minimized.

Show comment Hide comment
@frogFangio

frogFangio Oct 6, 2016

Hi, thanks for the code snippet. I'm just wondering what file I should place it in :-/

Hi, thanks for the code snippet. I'm just wondering what file I should place it in :-/

@db306

This comment has been minimized.

Show comment Hide comment
@db306

db306 Oct 10, 2016

@frogFangio it's written in Line 2. But in any case you can place this in any .php file within your theme. You could require a file in your inc folder and place it within 404debug.php so you don't end up with a 3000 line in functions.php

db306 commented Oct 10, 2016

@frogFangio it's written in Line 2. But in any case you can place this in any .php file within your theme. You could require a file in your inc folder and place it within 404debug.php so you don't end up with a 3000 line in functions.php

@salvatorecapolupo

This comment has been minimized.

Show comment Hide comment
@salvatorecapolupo

salvatorecapolupo Apr 22, 2018

Great code, saved my day. Thanks!

Great code, saved my day. Thanks!

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