One of the examples using add_rewrite_rule function in wordpress is


I can understand where matches[1] is and where matches[2] is, but is there any way to change these matches, something like:

add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=' . f1($matches[1]) . '&variety=' . f2($matches[2]),'top');

Where f1 is a function of matches[1] and f2 is a function of matches[2]

Thank you very much.

Edit 1:

This is a very general question with the objective to understand wordpress add_rewrite rule. One example of f1 and f2 function is

function f1($string) {
if (strpos($string, '/america/')) {$string .= '-1';}
esleif (strpos($string, '/england/')) {$string .= '-2';}
return $string;

f2 was a similar function to f1.


Rewrite rules don't call functions like you're asking. WordPress is far more flexible than that, because you might want to run your code at any one of an unlimited set of points in the processing of the page and not be restricted to that one specific moment in the code when the rewrite rule is processed.

Taking it back a step, rewrite rules are meant to work just like $_GET does, where you can pass data through the URL, like domain.com/?id=123. The difference is that the id= is determined by the rewrite rule, so you can have a pretty url like domain.com/123/. Oftentimes we'll keep the id in the URL, e.g. domain.com/id/123/ to avoid conflicts with other URL structures, but that's not important for the sake of this answer.

Using your example, say you had a rewrite rule for nations, and you wanted your URLs to look like /nation/america/ or /nation/england/. Here is an example rewrite rule for this:

add_rewrite_rule( '^nation/([^/]*)/?', 'index.php?nation=$matches[1]' );

To use this information, you can pretty much hook into any action or filter after parse_query. That's the first action available after the query vars are set, so it's the first action where you can use get_query_var. Here's an example:

function wpse_99414_get_nation() {
    if ( $nation = get_query_var( 'nation' ) ) {
        # This is the equivalent to your f1 function
        # $nation will either be 'america', 'england', or whatever else
        # process it however you need!
add_action( 'parse_query', 'wpse_99414_get_nation' );

Lastly, note that in order to use a custom query var like how we used nation, you need to register it. Here's an example of doing that:

function wpse_99414_query_vars( $qv ) {
    $qv[] = 'nation';
    return $qv;
add_filter( 'query_vars', 'wpse_99414_query_vars' );
  • I think this is an excellent answer and very detail explanation. Thank you very much :) – aye Jun 3 '13 at 14:49
  • @aye Happy to help! – Matthew Boynes Jun 3 '13 at 15:16

The function arguments are computed at the time of function call, so when rewrite rule is being registered rather than when it is being matched and used.

You will need to do processing on matched values at different point in code.

  • so do you know where to edit the code? – aye May 30 '13 at 12:09
  • @aye how would I? f1() and f2() give me nothing about what you are trying to achieve, what it depend on and what would be right place in load process to apply them. :) – Rarst May 30 '13 at 14:24
  • Hi Rarst, i have edited the question. Actually i'd like to know where to include the function f1 and f2 – aye Jun 2 '13 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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