How wordpress plugin hooks works? [duplicate]

do_action is an event and there are many of them throughout your WordPress instance, in the core or your themes and plugins. (Hypothetical) So even when you put an add_action in your plugin file but the theme files are read first by order of execution your add_action function will still fire at that event or time of the do_action it is hooked to.

This is because even the theme files are held before they are executed by a WordPress Core hook called after_setup_theme To know the order of all of these check out the documentation here

Essentially this means that all code should be inserted at some point in this action hook hierarchy, also you can create your own. So when adding code in a file you should be asking “when” in time is this executing? Have the theme files finished loading by this point? Has WordPress itself loaded yet? There are hooks for those things so be sure to read through that documentation