Rewrite rule never matching

Please try this.

First add the rewrite rule you need with this function

add_action( 'init', 'rewrite_site_tools_page' );
function rewrite_site_tools_page() {

  add_rewrite_rule(
    '^site-tools-([^/]+)$',
    'index.php?pagename=site-tools&tool=$matches[1]',
    'top');

}

Assuming that page name is site-tools.

Now add the tool as query var

add_filter('query_vars', 'wpd_query_vars');
function wpd_query_vars( $query_vars ){
    $query_vars[] = 'tool';
    return $query_vars;
}

Save permalinks.

Now if you access hxxp://yourdomain.com/site-tools-calculator/ It should work (calculator can be anything else )

If you want to debug it, you can dump the query var “tool” by this function:

add_action('template_redirect', 'test_my_queryvar', 999 );
function test_my_queryvar() {
    die( var_dump( get_query_var( 'tool' ) ) );
}

Leave a Comment