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 have a custom page and template, that I'm passing a query-string to. It has a wordpress permalink like so

/mypage/

I want to be able to add another rule so that the permalink receives a slug

/mypage/this-slug

is rewrote to

/mypage/?myslug=this-slug

share|improve this question

1 Answer 1

up vote 3 down vote accepted

This should work for you-

function wpse28906_rewrites_init(){
    add_rewrite_rule(
        'mypage/([^/]+)/?$',
        'index.php?pagename=mypage&myslug=$matches[1]',
        'top' );
}
add_action( 'init', 'wpse28906_rewrites_init' );

function wpse28906_query_vars( $query_vars ){
    $query_vars[] = 'myslug';
    return $query_vars;
}
add_filter( 'query_vars', 'wpse28906_query_vars' );

You can then use get_query_var( 'myslug' ) in your template to get your slug value. Don't forget to flush your rewrite rules after adding this by visiting the permalinks settings page.

share|improve this answer
    
how do i flush the rewrite rules? I visited the page and hit save. I cant access get_query_var( 'myslug' ). –  madphp Sep 19 '11 at 18:04
    
@madphp - just visiting the permalinks settings page flushes them. echo get_query_var( 'myslug' ) should echo the value of myslug if it's set. if you visit /mypage/anything/ and don't get a 404 or redirected, then the rewrite rule is working. –  Milo Sep 19 '11 at 18:22
    
do I need to use add_rewrite_tag somewhere? codex.wordpress.org/Rewrite_API/add_rewrite_tag . I dont get a 404, by going to /mypage/anything/ but i cant access the myslug query_var. –  madphp Sep 19 '11 at 18:23
    
Im using WordPress 3.0.4 –  madphp Sep 19 '11 at 18:40
    
I can access query variable like this. $wp->query_vars['myslug']; Is that acceptable? –  madphp Sep 19 '11 at 18:48

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.