admin管理员组文章数量:1391991
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, , 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
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/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
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Mar 12, 2014 at 1:36 MatthewLeeMatthewLee 2481 silver badge8 bronze badges 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
Reset to default 0In 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!
本文标签: rewrite rulesaddrewriterule queryvar not being set
版权声明:本文标题:rewrite rules - add_rewrite_rule query_var not being set 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744694023a2620168.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论