Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created the page template which needs to take some GET variables, currently URL's looks like that:

http://example.com/coupons/?cat_id=25

I need to make that way:

http://example.com/coupons/25

I checked the WP_Rewrite documentation and playing a bit with that stuff, but can't make things work as except.

Regards, Chris

share|improve this question

1 Answer 1

up vote 0 down vote accepted

First you have to filter query vars and add your query var to the array:

add_filter( 'query_vars', 'wpa56345_query_vars' );

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

Then your rule which captures any digits after your pagename coupons and passes that as cat_id:

add_action( 'init', 'wpa56345_rewrites' );

function wpa56345_rewrites(){
    add_rewrite_rule(
        'coupons/(\d+)/?$',
        'index.php?pagename=coupons&cat_id=$matches[1]',
        'top'
    );
}

The above both go in your theme's functions.php. Make sure to visit permalinks settings page after adding rewrite rules to flush rules, or they will not take effect. If you put everything into a plugin, you can set it up to flush rules when they are first added.

anyway, in your template, you can then access the value of cat_id with:

echo get_query_var( 'cat_id' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.