Use add_action within template
There is no limitation to using add_action(), except that core must have it loaded already. Practically this means pretty much anywhere, but wp-config.php. While it is possible to do it in template, it’s not a common practice because of: Timing issues (your template part might be loaded after the hooks you need). Code base clarity … Read more