Understand Child Themes, Hooks, Actions and Filters in Five Minutes
What on earth are they?
Example time. A theme you like uses
<h2> tag for post titles. You hate that and want
The old, boring way is you open the theme files, starting from
index.php, changing all
<h2> tag to
<h1>, then moving to the other files:
category.php, doing the same thing over and over again, hoping you don’t miss anything.
That is not clever because a) it’s too much work, and b) when the theme is updated, you’re screwed.
Wouldn’t it be better if you could just a) write the change once, have it applied globally, while b) at the same time not modifying the theme’s code?
Yes. Yes of course. And you can do just that by mastering the art of child theming, Hooks, Actions and Filters.
Enter Child Theme
Child theme is a lazy developer’s best friend.
A child theme is everything its parent is, except for the changes you add that make it awesome-er.
When you use a child theme, WordPress will check whether it has the usual PHP files. You know,
header.php, the likes. If your child theme has it, great, WordPress will happily use it. If not, WordPress will smile at it knowingly and use its parent’s files instead.
BAM! Now you understand how parent-child theme works in WordPress.
Every theme can be a parent theme, but some are especially kind and beneficial to their children. They are usually known as theme frameworks. Ever heard of them?
They make the best parents for various reasons (good coding practice, built-in SEO), but in this case it is because they have hooks aplenty.
Hooks, Filters, Actions!
Hooks are marked places on a theme. The start of the comment area. The post meta. And so on.
Actions and Filters are the things you can do at those places.
Actions do stuff. Like showing a picture of kitten after your post title. Filters specifically deals with modifying texts (or HTML, same thing). Words go into your Filter and come out differently. Profanities get Filtered into ****s.
Let’s repeat that.
Hooks: places. Actions: doing things. Filters: changing texts.
Theme frameworks are smart because they are made to be modified. Those clever guys and girls who wrote them know that you want to add stuff between your post title and content, that you want to change the wordings on your comment forms, so they add tons of useful hooks in their frameworks for you to use in your child theme.
Hooks are provided by the parent theme. Actions and Filters you write yourself in your child theme’s
And that is all.
Think of an awesome theme to create. Or a neat feature to add. Use a theme framework as a parent. Create a child theme. Use parent’s Hooks to add Actions and Filters. Joy.
Once you grasp the concept, visit these places to learn the technical sides.
How To Modify WordPress Themes The Smart Way by Ian Stewart. Explains all of these concepts with easy to follow examples.
Adding Custom WordPress Hooks by Benedict Eastaugh. For when you want to create your own theme framework.
Plugin API : Hooks, Actions and Filters at the WordPress Codex. Plugin? Yeah. Hooks, Actions and Filters were initially features that has to do with plugin creation.