Daniel Koczuła
Daniel Koczuła

Reputation: 1034

WP custom rewrite rule

I've got URL: http://example.com/alternatywy/?id=getresponse

And I'm trying to rewrite it to: http://example.com/alternatywy/getresponse

I've added to my functions.php:

add_action('init', 'dcc_rewrite_tags');
function dcc_rewrite_tags() {
    add_rewrite_tag('%id%', '([^&]+)');
}

add_action('init', 'dcc_rewrite_rules');
function dcc_rewrite_rules() {
    add_rewrite_rule('^alternatywy/(.+)/?$','index.php?page_id=8286&id=$matches[1]','top');
}

But it's not working. What am I doing wrong?

Upvotes: 0

Views: 428

Answers (1)

Neeraj Krishna Maurya
Neeraj Krishna Maurya

Reputation: 159

Try this:

add_filter('query_vars', function($vars) {
    $vars[] = "id";
    return $vars;
});

add_action('init', 'dcc_rewrite_rules');
function dcc_rewrite_rules() {
    add_rewrite_rule('^alternatywy/([^/]+)/?$','index.php?page_id=8286&id=$matches[1]','top');
}

Now its time to flush rewrite rules. You can do it within two ways either by code or by manual save permalink.

Manual:

From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

Code:

/* Flush rewrite rules for custom post types. */
add_action( 'after_switch_theme', 'flush_rewrite_rules' );

Upvotes: 1

Related Questions