[wp-source] What is "priority"?

Hi, I’m still in the learning stages of Frontity and a little confused about the priority param used in wp-source handlers and redirections. The docs say it’s the order in which the handlers/redirections are run in, which makes sense, but I have a few more questions just to understand it a bit more:

  1. Why is 10 used in most/all examples, is there any importance to that?
  2. Are they run in ascending or descending order? I’d assume ascending but confused why the examples use 10 and not 1 when there’s only one handler per example
  3. What’s the community’s usual way of assigning these numbers? Hardcoding? Dynamic basic on array index (which I kind of wish would be the default)?
  4. Do two handlers with the same priority run in parallel?

Hi there! Let’s see if I can help you understand how priority works.

Priority is important when the patterns used to match different handlers might return true for the same route. For example: /category/:slug and /:taxonomy/:slug both will match the route /category/my-category-slug.

To go into more detail, in this case we have a more specific pattern (/category/:slug), only for the taxonomy category, and a more general pattern (/:taxonomy/:slug), for any other taxonomy, therefore you want to give the more specific pattern a higher priority so it’s tested against the route before the general one, because once a pattern matches, no other handlers will be tested against that route.

If the handlers can’t collide between them (like /category/:slug and /tag/:slug), they can have the same priority because there is no risk of one handler being used with the wrong route.

The lower the number, the higher the priority. Consequently a handler with priority 5 will be tested before a handler with priority 10.

The reason why the default handlers have a priority of 20 is because it’s impossible to foresee the cases a developer might run into so, this way there is room for custom handlers to run before or after the default ones as needed.

You hardcode the priority on your custom handler. This mechanism is not something exclusive to Frontity, priority is used in the same way with WP actions, which is one of the reasons it was adopted in Frontity, we thought it would be familiar to people coming from WP.

No, they run in the order they are defined in the array.

2 Likes