Rewrite Rules Viewer and URL Tester
Adds Tools > Rewrite Rules, listing every rewrite rule and showing which rule and query a URL path you enter would match, for debugging 404s and custom permalinks.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
add_management_page( 'Rewrite Rules', 'Rewrite Rules', 'manage_options', 'wpide-rewrite-rules', function () {
$rules = (array) get_option( 'rewrite_rules' );
$test = isset( $_GET['wpide_path'] ) ? sanitize_text_field( wp_unslash( $_GET['wpide_path'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification -- read-only.
$path = trim( (string) wp_parse_url( $test, PHP_URL_PATH ), '/' );
echo '<div class="wrap"><h1>Rewrite Rules</h1>';
if ( ! get_option( 'permalink_structure' ) ) {
echo '<div class="notice notice-warning"><p>Plain permalinks are active, so WordPress uses no rewrite rules.</p></div>';
}
echo '<form method="get"><input type="hidden" name="page" value="wpide-rewrite-rules"><label for="wpide-path"><strong>Test a path</strong></label> <input type="text" id="wpide-path" name="wpide_path" class="regular-text" value="' . esc_attr( $test ) . '" placeholder="/category/news/page/2/"> <button class="button">Test</button></form>';
$matched = '';
if ( '' !== $test ) {
foreach ( $rules as $regex => $query ) {
if ( preg_match( '#^' . str_replace( '#', '\#', $regex ) . '#', $path, $m ) || preg_match( '#^' . str_replace( '#', '\#', $regex ) . '#', urldecode( $path ), $m ) ) {
$matched = $regex;
$resolved = preg_replace_callback( '/\$matches\[(\d+)\]/', function ( $x ) use ( $m ) {
return $m[ (int) $x[1] ] ?? '';
}, $query );
echo '<p>Matches <code>' . esc_html( $regex ) . '</code> → <code>' . esc_html( $resolved ) . '</code></p>';
break;
}
}
if ( '' === $matched ) {
echo '<p>No rule matches this path, so it would return a 404 unless a plugin handles it.</p>';
}
}
echo '<p>' . esc_html( count( $rules ) ) . ' rules, in the order WordPress checks them.</p><table class="widefat striped"><thead><tr><th>Rule</th><th>Query</th></tr></thead><tbody>';
foreach ( $rules as $regex => $query ) {
printf( '<tr%1$s><td><code>%2$s</code></td><td><code>%3$s</code></td></tr>', $regex === $matched ? ' style="outline:2px solid #2271b1"' : '', esc_html( $regex ), esc_html( $query ) );
}
echo '</tbody></table></div>';
} );
} );
Notes
Read-only: it never changes rules. Enter a path such as /events/summer-fair/.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.