admin管理员组文章数量:1316665
I'm making SEF urls for my plugin. To logic is: if there is my_shortcode on the page, then pass the rest of url as parametr dir= This code is perfectly works on one of my wordpress installations and not working on another. I got 404. What can be wrong on it?
add_action('init', array( $this, 'do_rewrite' ));
public function do_rewrite()
{
$page = get_page_by_path($_SERVER['REQUEST_URI']);
if (! isset($page->ID)) {
preg_match('#^/?(.*?)/(.+)/?$#', $_SERVER['REQUEST_URI'], $matches);
$parent_page = get_page_by_path($matches[1]);
if ($parent_page) {
$content = $parent_page->post_content;
if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {
add_rewrite_rule('^/?(.*?)/(.+)/?$', 'index.php?pagename=$matches[1]&dir=$matches[2]', 'top');
add_rewrite_tag('%dir%', '([^&]+)');
}
}
}
}
P.s. redirect rules flushed
UPDATE: Anyone can explain me what's happening? I commented those 2 if's:
// if ($parent_page) {
// if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {
and redirects starts working. Then I returned it back.. and it keeps working!
I'm making SEF urls for my plugin. To logic is: if there is my_shortcode on the page, then pass the rest of url as parametr dir= This code is perfectly works on one of my wordpress installations and not working on another. I got 404. What can be wrong on it?
add_action('init', array( $this, 'do_rewrite' ));
public function do_rewrite()
{
$page = get_page_by_path($_SERVER['REQUEST_URI']);
if (! isset($page->ID)) {
preg_match('#^/?(.*?)/(.+)/?$#', $_SERVER['REQUEST_URI'], $matches);
$parent_page = get_page_by_path($matches[1]);
if ($parent_page) {
$content = $parent_page->post_content;
if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {
add_rewrite_rule('^/?(.*?)/(.+)/?$', 'index.php?pagename=$matches[1]&dir=$matches[2]', 'top');
add_rewrite_tag('%dir%', '([^&]+)');
}
}
}
}
P.s. redirect rules flushed
UPDATE: Anyone can explain me what's happening? I commented those 2 if's:
// if ($parent_page) {
// if (preg_match('/\[my_shotrcode(.*?)\]/i', $content)) {
and redirects starts working. Then I returned it back.. and it keeps working!
Share Improve this question edited Nov 9, 2020 at 13:22 Movs Ovs asked Nov 9, 2020 at 9:52 Movs OvsMovs Ovs 12 bronze badges 01 Answer
Reset to default 0Looks like the problem is flushing rules. Flushing rules from Settings-Permalinks doesn't help somehow, so I did it in code like this:
if (! in_array('index.php?pagename=$matches[1]&dir=$matches[2]', get_option('rewrite_rules'))) {
flush_rewrite_rules();
}
本文标签: url rewritingSEF addrewriterule amp addrewritetag not working
版权声明:本文标题:url rewriting - SEF add_rewrite_rule & add_rewrite_tag not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742007253a2412209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论