4

I must say that I am a complete nooby at Wordpress custom URL rewrites. I have searched for past many days to find a clear explanation of how to determine and write correct pattern for URL rewrites. I have a custom page template that uses query var passed to it, e.g. http://example.com/pagename?user=username. In this "pagename" refers to the custom page template and "user" refers to the custom query var. I need this to be represented using the URL http://example.com/pagename/username.

I also require the above to work with pagination. So http://example.com/pagename/username/page/2 should be able to represent http://example.com/pagename/page/2?user=username

It would be great if someone can provide me with a working example and an explanation of how I should determine and write correct pattern for URL rewrites.

Regards, John

2
  • 1
    Will pagename be dynamic, or a 'known', static slug? Oct 11, 2011 at 22:54
  • @TheDeadMedic Thx for the reply. "pagename" shall be a known static slug.
    – John
    Oct 12, 2011 at 19:47

2 Answers 2

2

Extending @Stephen Harris' excellent answer, I would opt for;

add_action( 'generate_rewrite_rules', 'my_rewrite_rules' );
function my_rewrite_rules( $wp_rewrite )
{
    $wp_rewrite->rules = array(
        'mypageslug/([^/]+)/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?pagename=mypageslug&user=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
        'mypageslug/([^/]+)/?$' => $wp_rewrite->index . '?pagename=mypageslug&user=' . $wp_rewrite->preg_index( 1 )

    ) + $wp_rewrite->rules;
}

This follows the defacto regex standard used in WordPress core. The main change from Stephen's code sample is the use of $wp_rewrite->index to ensure the rewrite is passed through to WordPress (otherwise it may get added to the 'external'rewrite list).

Secondly, and most importantly, passing the pagename var in the query string - this means WordPress will behave as if it were ordinarily loading a page with the slug mypageslug.

UPDATE: I forgot to add, make sure user is a public query var, otherwise WordPress won't map it from the URL. Either filter it in with query_vars or add it using the global $wp;

add_filter( 'query_vars', 'my_query_vars' );
function my_query_vars( $vars )
{
    $vars[] = 'user';
}

// my preferred method - make sure you run this only after 'init' has fired!
$GLOBALS['wp']->add_query_var( 'user' );

// then once the request has been parsed (anything after the action 'parse_request')
$user = get_query_var( 'user' );
1
  • It works like a charm. Thx for the inputs!
    – John
    Oct 13, 2011 at 14:37
0

I have had a similar problem and successfully solved it. In fact I recently wrote this blog post which used this solution.

WordPress uses rules to make the URls pretty, the idea is to use a filter to call a function that add rules (then after going to settings > Permalinks and clicking save the rules should be added). This is the code:

add_action('generate_rewrite_rules', 'my_rewrite_rules');
function my_rewrite_rules( $wp_rewrite ) {
  $new_rules = array(
     'pagename/(.+)' => 'pagename?user='.$wp_rewrite->preg_index(1),
       );

  // Add the new rewrite rule into the top of the global rules array
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

The idea is that WordPress checks to see if the url is of the form www.example.com/pagename/something and uses that something to set the user variable.

To get pagination to work you'll need to add the following rule to the $new_rules array - above the rule already there (this is because WordPress checks the rules in order).

    'pagename/(.+)/page/?([0-9]{1,})' => 'pagename?user='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2),

Similarly this checks for www.example.com/pagename/something/page/2 say and interprets this as example.com/pagename?user=username&paged=2. (Paged is the query variable recognised by WordPress for determining the page in pagination).

Hope this helps!

4
  • Thx for the reply. I read through your blog post and found it informative. However, the solution you suggested displays the homepage instead of the custom page template. I have flushed the rewrite rules. Any idea what could be going wrong here?
    – John
    Oct 12, 2011 at 19:46
  • @user9094 Check out my answer, which follows on from Stephen's. Oct 12, 2011 at 21:37
  • @user9094 - what happens when you try example.com/pagename?user=username? The solution above simply re-writes the url, so if you go the above to work, then example.com/pagename/username should also work. Oct 12, 2011 at 22:12
  • @StephenHarris example.com/pagename?user=username does work. I used TheDeadMedic's extended code and now it works absolutely perfect. Thx for your inputs!
    – John
    Oct 13, 2011 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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