SEF add_rewrite_rule & add_rewrite_tag not working

Question

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

0
Movs Ovs 3 months 0 Answers 11 views 0

Leave an answer