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.

Theme Options UI: Consistency Matters!

Just tweeted this:

Doing it right: #genesiswp ‘s theme options UI mimics WP’s look instead of having its own fancy UI.

Consistency Matters

A lot of you must have seen how various WordPress themes have different theme options UI design. Most of them opt for pretty looking options page with labels, inputs, buttons, icons, and forms that look wildly different from the rest of the Dashboard.

If your theme is like that, you’re doing it wrong.

Huh? Why?

Matt: Gravatars aren’t going to hurt your Google rankings

This site’s current design sucks when it comes to showcasing new comments, so in the meanwhile here’s Matt clarifying on a old post about whether Gravatar slows down page load speed and hurt search engine rankings:

…Gravatars (or any images) aren’t going to hurt you in the Google rankings or we would have seen a massive drop-off in traffic on WP.com.

True, because as far as I know page load speed isn’t taken into account yet. And when it is, Gravatars will be just one of the many other factors that influence load speed anyway.

A Collection of WordPress-based Short URLs

Nowadays pretty much everybody is using pretty permalinks while also using the much shorter url.com?p=page_id format for the short URL of the same post, usually on Twitter. You can also use the various URL shortening services, of course, but Zeldman’s article explains why it’s good to roll your own short URL:

Rolling your own mini-URLs lessens the chance that your carefully cultivated links will rot if the third-party URL shortening site goes down or goes out of business

This post will show you a few more unique keywords (not just “p”) that you can use to shorten your URL with WordPress. You don’t need to do anything to get these keywords to work; they’re available to any WordPress install and will still work regardless of your Permalinks setting.

p

Links to a certain Post / Page.

Usage: url.com/?p=(Post or Page’s ID)
Example: wplover.com?p=1426

m

Either links to Posts from an entire year or an entire month.

Usage:

  • To link to a certain month’s Posts: url.com/?p=(YYYYMM)
  • To link to a certain year’s Posts: url.com/?p=(YYYY)

Example:

  • wplover.com/?m=201001
  • wplover.com/?m=2010
  • cat

    Links to Posts from a certain category.

    Usage: url.com/?cat=(Category ID)
    Example: wplover.com?cat=3

    tag

    Links to Posts tagged with a certain tag. Note that you use slug instead of ID here.

    Usage: url.com/?tag=(Tag slug)
    Example: wplover.com?tag=awesome

    s

    Links to search results for your supplied keyword.

    Usage: url.com/?s=(Keyword)
    Example: wplover.com?s=design

    Any other short URL keyword I missed?

    This is a Post on a WordPress Design Blog that Talks about Super Mario Bros.

    Sounds silly, I know, but here’s a nice quote on “Great products are triumphs of taste” by 37signals:

    Want to build a great iPhone app? Go listen to Billie Holliday. Trying to design a piece of hardware? Visit a Frank Lloyd Wright house. Aiming to write great marketing copy? Read Aldous Huxley. Need a color scheme? Go to the museum and check out some Mark Rothko paintings.

    So. Super Mario Bros.

    Read more →

    Theme Developers, Learn to Steal the Right Way!

    I’ve talked about this on WPTavern before, but Leland’s tweet made me feel that we should talk about this more.

    Remember when LogoMaid ripped of Dan Cederholm’s logo? Everybody pretty much agreed that that was illegal. And so imagine my surprise knowing that there are WordPress themes that are direct copies of Twitter, of Facebook, of Basecamp, and what have you. Heck, we even have a theme describing itself as “The exact Facebook clone theme for WordPress” in the official Theme Directory. This I believe is a case worse than the LogoMaid issue.

    No clone themes, please.

    Please stop this. The freedom in GPL does not mean the freedom to steal copyrighted design. Stop making clones of popular websites and turning them into WordPress themes. It doesn’t matter if you release it only for personal use, or under GPL, if you code the CSS yourself, if you painstakingly recreate the graphic elements in Photoshop. It’s still, as Ryan Hellyer puts it, “illegal, immoral, and unethical.”

    Instead of doing that, go and read this article by Cameron Moll, “Good Designers Copy, Great Designers Steal“, and learn how to “steal” a design in a much better (and ethical) way. Learn what makes them work, and improve it:

    This article wouldn’t be complete without a warning to be careful when copying well-known sources. If I were to summarize this warning in one sentence, this would be my golden verbiage: copy the inspiration, not the outcome.

    Or teach us. Write an article on how you do that AJAX load more posts wizardry. Or how to make that rounded corner work on every browser. Show us how to recreate your favorite website’s cool feature in WordPress.

    Now that will be awesome.

    Update: Another discussion is up at Theme Lab, WordPress Clone Themes – Your Take?

    How Do You Keep Track of Theme Usage?

    A question: how do you guys keep track of sites that use your theme?

    I find that Google’s Webmaster Tools show links to my site, including those from the link on the footer area of my themes, but its update can be quite slow and incomplete.
    Sample of Google Webmaster Tools link to site area

    What about you guys?

    Page 1 of 512345

    Latest Links More →

    Custom Shortlinks for WordPress

    Have your own short domain name for the purpose of shortlinking? Here’s an easy way to combine that with your WordPress install.

    The Quick Start Guide to Using Google Webmaster Tools With WordPress

    GWT is a great, frequently updated features like showing you search queries volume, malware and crawl error diagnostics and links to your site. If you don’t use it yet, you probably need to. This will help you get started.

    WordPress & jQuery Contact Form without a Plugin

    I would recommend this either if you want more flexibility or to learn how to code a contact form.

    Understanding and cleaning the pharma (spam) hack on WordPress

    How to fix that hack:

    This attack is very interesting because it is not visible to the normal user and the spam (generally about Viagra, Nexium, Cialis, etc) only shows up if the user agent is from Google’s crawler (googlebot). Also, the infection is a bit tricky to remove and if not done properly will keep reappearing.

    Web Safe Fonts Cheat Sheet

    An updated (written in April 2010), well researched, CC-licensed Web safe fonts cheat sheet, available both in low-res PNG and high-rest PDF. Even the article is useful as well.

    The Nicest 2010 Child Theme You’ll See Today

    The Timaru Mental Health Support Trust website, made for charity by Team USA (comprised by web superstars like Jason Santa Maria, Dan Mall, Liz Danzico and Automattic’s John Ford) during the FullCodePress competition, is actually a clever child theme of 2010.

    More recap by JSM, Daniel Mall, and Liz Danzico.

    WordPress 3.0 Theme Tip: The Comment Form

    The simpler way to code comment form (once you understand how hooks and filters work).

    Showing and hiding content with pure CSS3

    I like it, I think it’s short and easy to understand.