• Resolved damiarita

    (@damiarita)


    In this website uniquecorners.com I have created a custom post type called ‘tour’ with a taxonomy ‘destination’. I wanted the URL to be domain/destination-slug/tour-slug (for a tour page) or domain/destination-slug (for a destination page). So I wanted to avoid putting ‘tour’ in the URL.

    To accomplish this, I ignored the rewrite parameters when I register the post type, as I didn’t find a way to do this. In stead, I create the extra rewrite rules myself.

    I see two ways of doing it and I am not sure which is better.

    This is the way I do it now (based on https://www.ads-software.com/plugins/hierarchical-urls/):
    I use the filter rewrite_rules_array. I basically prepend all my rules to it.

    This is the alternative I am considering:
    Using some action and calling add_rewrite_rule

    What pushes me towards this alternative is that it looks more “official”. I like using the API.

    What pushes me away from it is that using an action makes it execute the code every time. In the filter I am not sure it is executed every time. It might only be executed when rules are flushed (this is a complet guess).

    So, my questions are:

    1. Is it true that this filter is only used when rules are flushed?
    2. What action would you use if I used the API?
    3. Is there an action I can use that doesn’t fire when a cached page is served (I use w3total cache)?

    Thanks!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    There are multiple ways you can add the rewrite rules. All are part of the API. It doesn’t matter if you use add_rewrite_rule in the init action or use the rewrite_rules_array filter as you’re doing now. Both are intended to add or filter rewrite rules. Personally I think add_rewrite_rule is better as it’s the intended way to add the rules and does some extra things in the background.

    Both are run on every pageload so WordPress can use it to update the rewrite rules with your rules whenever it’s needed (flushed + saved to the database).

    It doesn’t matter if it’s executed on every pageload though. WordPress needs the rules when they are flushed and regenerated. The only thing you should avoid is flushing the rules on every pageload.

    Thread Starter damiarita

    (@damiarita)

    Thank you keesiemeijer!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding rewrite rules’ is closed to new replies.