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 <h1> instead.

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: archives.php, tag.php, category.php, doing the same thing over and over again, hoping you don’t miss anything.

Gah!

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, index.php, 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. <h2>s to <h1>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 functions.php.

And that is all.

Recap

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.

Further reading

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.

You may also like...

6 Responses

  1. Rollie says:

    Thanks for your explanation on parent/child themes in simple terms. I am new to WordPress (after creating a few websites from scratch using Notepad in the mid to end 90s and not much since) and now understand the advantage of this concept, however I just wonder if creating child themes can be mastered by non-coders just as easily as working on the Admin panel of these frameworks (i.e. Frugaltheme, iTheme Builder, Headway)?
    You are saying “Hooks are provided by the parent theme. Actions and Filters you write yourself in your child theme’s functions.php”
    Writing = coding? Is creating and working on a child theme something that could not be provided via a user friendly UI where the Actions and Filters just work in the background in conjunction with the parent theme (framework)?

  2. Hafiz Rahman says:

    I just wonder if creating child themes can be mastered by non-coders just as easily as working on the Admin panel of these frameworks (i.e. Frugaltheme, iTheme Builder, Headway)?

    No, I would say your examples above are far easier to use for non-coders than creating child themes. Child themes varies, from a simple CSS-only child theme to the more complex ones with filters and actions, but nevertheless creating one will require you to be familiar with HTML, CSS and PHP.

    Writing = coding?

    Yes, sorry for being unclear. I meant you code them yourself.

    Is creating and working on a child theme something that could not be provided via a user friendly UI where the Actions and Filters just work in the background in conjunction with the parent theme (framework)?

    This is actually a very nice idea, but I haven’t seen this implemented that well. The Simple Hooks plugin for the Genesis frameworkis a good example, but even with that you will still need to at least know HTML so it probably won’t help much for non-coders. I am unaware of a plugin that works with any frameworks, though.

    Basically the idea of Actions and Filters is that it helps immensely for developers, but for non-coders it’s probably still not the best solution.

  3. Rollie says:

    So what is everyone else doing when they want a fully customizable theme that allows to adjust layout and design in every aspect, especially with view to the many upgrades that the underlying theme (framework) requires in order to maximise full use of new features?

    Are people simply not upgrading because it’s all too complicated? Or are many people in fact losing all their customized themes when they do upgrade, because they did not know this would happen?

    I thought the use of a parent and child theme concept was based upon that very idea, so that some core files (the ones being replaced during an upgrade) will not be affected by modifications to the files attached to the child theme (or in other words you don’t lose your customized theme during an upgrade)?

    It sounds so easy in theory, but really I don’t understand why creation of child themes should be difficult to be integrated into the admin panel/user interface just like all the other functions like layout/style/design/seo modifications?

    Just so someone wanting to use the tools visually and concentrate on design and content rather than programming, can actually enjoy this?

  4. Hafiz Rahman says:

    It’s true that another benefit for parent-child theme is that you can upgrade the parent theme without breaking the child theme that you’re using. However, based on my observation, themes are rarely updated save for the framework ones or if the theme author is particularly dedicated with her theme.

    I don’t know how many people upgrade vs those who don’t, but what I can say is as long as you’re using the parent-child system correctly, updating the parent theme should not cause any problem.

    I think you have a great idea there that child themes creation could be made easier for non-coders by creating a UI for various modifications, but as far as I can tell nobody has made this yet. For now coding is unavoidable.

  5. Really nicely written.And this theme is rocking.

  1. September 18, 2010

    […] в случая е, че за първи път подходих към т.нар. child theme concept, и на практика това, което виждате в момента, е дъщерна […]

css.php