• Resolved chiqui3d

    (@chiqui3d)


    I’m trying to modify the Woocommerce slugs with the object $wp_rewrite. When I modify it, everything works as I put it, but the rest as the pages stop working. Here the code:

    public function rewrite(): void {
            global $wp_rewrite;
    
            $wp_rewrite->add_rewrite_tag("%category%", '([^/]+)', "category=")
            $wp_rewrite->add_permastruct( 'category', '%category%' );
            $wp_rewrite->add_rewrite_tag("%product_cat%", '([^/]+)', "product_cat=");
            $wp_rewrite->add_permastruct( 'product_cat', '%product_cat%' );
            $wp_rewrite->add_rewrite_tag("%product%", '([^/]+)', "product=");
            $wp_rewrite->add_permastruct( 'product', '%product_cat%/%product%' );
        
    }

    This function is executed with the hook init.

    Does anyone know why it doesn’t work or is it a bug?

    I also tried modifying the array of extra_permastructs, but it doesn’t work either.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    When your permastructs don’t have a base element like 'cat/%category%', the resulting rewrite rule will match page requests before the proper page rewrite rule is encountered, causing WP to make the wrong query for the request. It’ll try to find a category using the requested page name instead of a page. As you have multiple permastructs with no base element, WP gets very confused about how to handle the request.

    Thread Starter chiqui3d

    (@chiqui3d)

    Thank you very much for your great response. Understood, but what I don’t understand when I add a permastructs, it doesn’t add a regex rule, that is if I manually add a rule without using permastructs it works because it checks all the rules. I understand that I will have to make a lot of modifications because permastructs don’t work well, which I don’t understand.

    Moderator bcworkz

    (@bcworkz)

    Without a permastruct base element, the regexp ([^/]+)/?$ is added to the $wp_rewrite->rules property, assigning the capture to category= (or whatever the particular tag defines) query var. This will match simple page requests as well, assigning the page name to category=. This rule occurs before the default page regexp, thus page requests will fail. Maybe you missed the addition, or you didn’t check late enough, but permastruct regexps are added to rewrite rules. Otherwise page requests wouldn’t have become broken.

    If you add a base element like 'cat/%category%', the added regexp becomes cat/([^/]+)/?$, which will not conflict with the default page regexp. WP tries to match every request to the entire list of regexps. As soon a s a match is found, it rewrites to the associated URL (root index.php), assigning regexp captures to query vars like category=

    If there are redundant regexps, WP will not try them all to see which will work, it goes with the first match found whether it works or not. If this does not help in your understanding, could you better describe what it is you’re confused about?

    Thread Starter chiqui3d

    (@chiqui3d)

    Understood. Thank you very much for your time and great explanation.

    I’m going to try to check some plugins that do that, without having to put any base.

    • This reply was modified 4 years, 4 months ago by chiqui3d.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘When I modify the product’s permalink, the pages stop working’ is closed to new replies.