0

My rewrite rule to make a convenient url for deep-linking into podcasts

add_filter("rewrite_rules_array", function($rules) {

    $newRules = array();

    $newRules["(.*)/time/([\d|\:]+)s?$"] = 'index.php?name=$matches[1]&t=$matches[2]'; 

    $merged = array_merge($newRules, $rules);

    return $merged;

});

Query Monitor shows me this rewrite rule is working; I get to single.php with the right post, Woop Woop!

But $_GET["t"] is not set. I also don't see a t in $wp_query->query_vars. How can I now make use of t inside the single.php?

.

.

What confuses me: My rewrite rule for /email works similar; but email.php can make use of the get-vars;

add_rewrite_rule(
    '^email/(.*)/(.*)/?$',
    'wp-content/themes/myTheme/email.php?to=$1&subject=$2',
    'top'
); 
1
  • 1
    You have to introduce the variable with add_query_var. Dec 16, 2017 at 13:03

1 Answer 1

0

Nicolais Tip got me on the right track; Here is the working code:

// add a query_var first
add_filter("query_vars", function($vars) {
    $vars[] = "myTheme_podcast_time";
    return $vars;   
});

add_filter("rewrite_rules_array", function($rules) {

    $newRules = array();
    $newRules["(.*)/time/([\d|\:]+)s?$"] = 'index.php?name=$matches[1]&myTheme_podcast_time=$matches[2]'; 

    $merged = array_merge($newRules, $rules);
    return $merged;

});

And then in single.php:

$time = get_query_var("myTheme_podcast_time");

// do stuff

Your Answer

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

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