2

I have an unusual use of add_rewrite_rule (is there a usual use?) and I am getting some unexpected behavior. First here is my code:

if(!empty($list_view_template))
    add_rewrite_rule(
        "{$list_view_template}/(.*)?",
        'index.php?mlscrit=$matches[1]/list-view/&pagename=' . $list_view_template,
        'top'
    );

add_filter('init', 'mls_declare_mls_custom_vars');

function mls_declare_mls_custom_vars() {
    add_rewrite_tag( '%mlscrit%', '(.*)' );
}

so as you can see I am using a pagename set within the admin dashboard to a variable called $list_view_template to limit a rewrite rule. This works fine so long as I flush rewrite rules after I update the value (which I do). The problem is with the query variable mlscrit. When I run this through Jan Fabry's excellent rewrite analyzer, https://wordpress.stackexchange.com/a/3608/27556, it gives me what I expect, mlscrit = '/list-view/' plus whatever is in the regex even if the regex is empty.

However if I do this:

add_action( 'template_redirect', 'mls_feed_rewrite_catch' );
function mls_feed_rewrite_catch()
{   
      echo 'mlscrit: ' . get_query_var('mlscrit' );
}

and the regex has something in it, it works. But if the regex is empty, the variable also loses the '/list-view/' part such that it is empty.

Can anyone explain to me why this would be the case?

I should also mention I have tried hardcoding the pagename, I have tried using the query_var filter in lieu of the add_rewrite_tag and I have tried grabbing the value of mlscrit in a bunch of different places and I always get the same answer, mlscrit is empty unless there is something in the regex.

Thank you!

Matthew

2
  • Ever found a solution for this?
    – user47133
    Commented Feb 9, 2015 at 23:20
  • @yivi, I did. I changed the order of the elements and it worked. I will post it as a solution.
    – MatthewLee
    Commented Feb 10, 2015 at 16:57

1 Answer 1

0

In response to yivi's inquiry, So I was able to solve the problem by putting the hardcode portion of the redirect string first as shown here:

add_rewrite_rule(
    "{$list_view_template}/(.*)?",
    'index.php?mlscrit=/list-view/$matches[1]&pagename=' . $list_view_template,
    'top'
   );

Thanks!

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.