0
votes

I want to add a simple custom Wordpress rewrite rule but somehow I don't get this working.

This URL

http://www.gewerbesteuer.de/steuer/muenchen should call this

http://www.gewerbesteuer.de/index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen.

So I want to call a page, which displays tax rates for a certain city, the city should be in the url as the last part.

Here is my code:

function custom_rewrite_tag() {
    global $wp_rewrite;
    add_rewrite_tag('%loc%', '([^&]+)'); 
    add_rewrite_rule('steuer/([^/]+)', 
        'index.php?pagename=gewerbesteuerhebesaetze&loc=$matches[1]', 'top');    
    flush_rewrite_rules();    
}

function query_vars($query_vars ) {
    $query_vars[] = 'loc';
    return $query_vars;
}

add_action('init', 'custom_rewrite_tag');
add_filter( 'query_vars', 'query_vars' );

The rewrite rule is working but the parameter (in this case loc) is not picked up. Even if I hardcode the rewrite rule with a certain city like

add_rewrite_rule('steuer/([^/]+)', 
    'index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen', 'top');

it still doesen't pick up the value of the loc parameter. I also noticed that the $matches array is empty and doesn't contain any values. I went through all the docs at wordpress and the questions here, but couldn't find the problem. Any ideas?

Thanks

Bernhard

1

1 Answers

0
votes

Can you try adding this and see if this makes any difference?

Edited

I have changed the paramter in the rewrite rule from "pagename" to "page_id" - I am assuming its 2207 as thats what it said from looking at the body class on your website's page.

I also removed flush_rewrite_rules() from the custom_rewrite_rules_tags function, can you try this now then go to Options > Permalinks and re-save again.

function custom_rewrite_rules_tags() {
    global $wp_rewrite;  
    add_rewrite_tag('%loc%', '([^&]+)'); 
    add_rewrite_rule(
        'steuer/([^/]+)', 
        'index.php?page_id=2207&loc=$matches[1]', 
        'top'
    );        
}
add_action('init', 'custom_rewrite_rules_tags');

function custom_query_vars($query_vars ) {
    $query_vars[] = 'loc';
    return $query_vars;
}
add_filter( 'query_vars', 'custom_query_vars' );