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!


Looks 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'))) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.