Important: Think of actions and hooks instead as events
That mental model will serve you far better.
Now, because I wanna extend that plugin, I'll hook into plugin_1_action, but what if my plugin was installed after that plugin?
You can install your plugins in any order you want, they are all loaded in the same order regardless. Installing plugin C first doesn't mean its code runs first.
Plugins are loaded alphabetically, so plugin a
will load before plugin b
because A comes before B, etc
Won't that plugin run first and so, I'll lose the chance to catch the plugin_1_action hook?
Correct, if the action is fired once, and that event has already occurred, then it's too late. You can still hook into it, but that event has already happened, so it will never run.
Similar to telling somebody to buy milk after they've been to the shop rather than before. You can do it, but it won't get you milk unless they visit the shop a second time.
For it to still work would need to involve some kind of time travel/temporal mechanics. Dr Who might be able to do it, but we'll require refactoring to fix the original plugin.
It's just weird and magical to me that I can, no matter what, hook into, say, Woo's actions and Woo can hook into mine,
Actions are just actions, they don't belong to anybody. There's no sandboxing of code, and once it's loaded it's loaded. A plugins code shares all the same space as WP Core, and other plugins.
even if clearly my plugin has been installed later than it.
Installation order is irrelevant. As we learnt earlier too, order of execution matters, once the event/action/hook is fired, you can still hook into it, but your functions won't run until it's fired again which might never happen. WP hooks don't do time travel.
Even if most plugins run on the init hook (so they get executed on the same space), they still have priorities set differently.
Indeed, if the event hasn't happened yet, you can indicate priority to influence the order the functions are called in.
Meaning that plugin1's code will run before plugin2's code and if plugin2 has a hook into plugin1, well, tough luck...or not, apparently WordPress doesn't have this issue.
Oh but it does! As I mentioned earlier, you can add functions to a hook that has already fired, that doesn't mean it retroactively applies it to previous times the hook fired. As I mentioned before, that would require time travel technology.
The root problem here is that the plugin is doing work before the init
or admin_init
hooks happen. When plugins load, they should set up their objects, add their hooks etc, but they shouldn't do work. That's what the lifecycle events of init
etc are for