get_template_part vs action hooks in themes

I prefer hooks, since they are more flexible: you can hook into them from your theme’s functions.php file, but also from plugins. I try to put as much logic in plugins, so that the themes contain mostly layout stuff.

If you use an action hook, it is still possible to use get_template_part() in that hook handler. This gives you the best of both worlds. You could probably even create a default hook that calls get_template_part(), so that people who don’t have much experience with coding can add extra files, and others can remove this hook if they don’t want to.

Regarding performance: get_template_part() uses (in locate_template()) file_exists() one, two or four times (depending on how you call it). It appears file_exists() is very fast, and uses caching in PHP and maybe even in the OS. So that is probably not an issue.

Leave a Comment