admin管理员组文章数量:1322176
okay i have made my own rewrite rule, with the add_rewrite_rule function in wordpress to get a custom url, for an plugin page.
add_action( 'init', function() {
add_rewrite_rule( 'login/?$', 'index.php?function=login', 'top' );
add_rewrite_rule( 'login/([a-zA-Z0-9]+)/?$', 'index.php?function=login&id=$matches[1]', 'top' );
});
add_filter( 'query_vars', function($query_vars) {
$query_vars[] = 'function';
$query_vars[] = 'id';
return $query_vars;
});
This works great and i can visit the page [domainname]/login to visit the page. Now the problem how do i best get a propper permalink for this page, one that works both with rewrite rules turned on, and off.
I tried using the get_permalink() function, and that works with pages, post on other stuff, but is there a function working in similar fashion that can get custom url's like this?
okay i have made my own rewrite rule, with the add_rewrite_rule function in wordpress to get a custom url, for an plugin page.
add_action( 'init', function() {
add_rewrite_rule( 'login/?$', 'index.php?function=login', 'top' );
add_rewrite_rule( 'login/([a-zA-Z0-9]+)/?$', 'index.php?function=login&id=$matches[1]', 'top' );
});
add_filter( 'query_vars', function($query_vars) {
$query_vars[] = 'function';
$query_vars[] = 'id';
return $query_vars;
});
This works great and i can visit the page [domainname]/login to visit the page. Now the problem how do i best get a propper permalink for this page, one that works both with rewrite rules turned on, and off.
I tried using the get_permalink() function, and that works with pages, post on other stuff, but is there a function working in similar fashion that can get custom url's like this?
Share Improve this question asked Sep 17, 2020 at 9:38 bymembymem 111 bronze badge1 Answer
Reset to default 1No, there is no permalink. Because it's a bespoke URL there's nothing to latch on to. You'd need to generate such a function yourself from scratch.
Luckily, your URL is just the word login/
with an ID on the end, so when you need a URL, just write out login/
and put the ID on the end. Wrap it in a call to site_url
to change it from a relative path to an absolute path.
For the non-pretty version, just take the 'index.php?function=login&id=$matches[1]'
and swap $matches[1]
for the ID you want.
本文标签: url rewritingGetting back a permalink from custom url
版权声明:本文标题:url rewriting - Getting back a permalink from custom url 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742115209a2421435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论