Using Actions/Hooks in Child Themes

So, if you’ve been using Child Themes for awhile, you might be very used to the normal way of modifying our themes. You take a file (like 404.php, archive.php, or another file in the root directory) and copy it into your child theme. Then you make a few modifications, and bam! It works.

This is awesome. But the problem is that it only works for the code in the root directory. These files in the root directory are called ‘templates’, and WordPress automatically makes the files child theme friendly.

The issue is that there is a whole other set of files that you can’t do this with. They are called ‘core files’. In Vantage (and other themes), this includes every file outside of the root directory (/includes/, /framework/, etc). These files are used for lots of different complicated jobs that go into making our themes so powerful. And just like it wouldn’t be a good idea to just start going in and replacing parts of WordPress, you don’t want to start replacing random parts of themes.

Hooks & Filters

But just because you can’t just go in and replace stuff, doesn’t mean you shouldn’t be able to customize how the theme works. That’s why WordPress has Hooks and Filters. They let you inject your code into certain parts of our themes without breaking things (most of the time).

The best way to learn about Hooks & Filters is to read about it on the WordPress Codex. Ignore the part where they call it the Plugin API, it works just fine for themes as well.

Read that? Cool. Let’s keep going.

Hooking into Vantage (or other themes)

Given you just read about hooks, you know that when you add a function to a certain event, WordPress will call that function when the event happens. For instance, I can hook a function to ‘save_post’, and it will be called every time the post is saved.

The cool part about this is it is just as easy to hook something as it is to unhook something. Let’s say the following code was in a core file in Vantage:

add_action( 'save_post', 'va_email_me' );
function va_email_me( $post_id, $post ){
    wp_mail( 'example@example.com', $post->post_title, 'This post was posted' );
}

Let’s say that you want the email to go to example1@example.com instead of example@example.com. Well, you can’t just go in and change the line of code because it would get overwritten the next time you update. You might be thinking, I wish AppThemes had put in a filter so I could change the email address.

Well, instead of posting a request and waiting for our response, you can simply use a child theme or function to unhook our function, and replace it with yours.

remove_action( 'save_post', 'va_email_me' );
add_action( 'save_post', 'my_email_me' );
function my_email_me( $post_id, $post ){
    wp_mail( 'example1@example.com', $post->post_title, 'This post was posted. YAY' );
}

As you can see, in our child theme’s functions.php, we simply made a call to remove the action that hooks ‘save_post’ and Vantage’s ‘va_email_me’ together, and added our own version of it.

This method works with almost all of our code. In Vantage, there are over 190 calls to ‘add_action’.

However, you should be careful with the things you unhook. We don’t put in any code in Vantage that we don’t deem necessary to making our product as awesome as possible. So, if you unhook some really core things (registering custom post types, taxonomies, etc), be prepared for some things to not work right.

Like this tutorial? Subscribe and get the latest tutorials delivered straight to your inbox or feed reader.

Your rating: none
Rating: 4.7 - 11 votes

Written by: on September 18, 2012. Last modified: September 22, 2012

Popular Marketplace Items

  • pcp-featured
    $39

    Price Comparer

     (3)
    Jump start your Price Comparision site with products from Amazon, Commission Junction, and LinkShare.
  • 2Checkout Payment Gateway Plugin
    $39

    2Checkout

     (3)
    Accept up to eight payment methods, fifteen languages, & twenty six currencies.
  • AppThemes Coupon Plugin
    $29

    AppThemes Coupons

     (8)
    An easy way to start offering coupons and promotions to your customers.
  • critic-icon-2
    $39

    Critic

     (6)
    A professional review and rating system for WordPress.
  • adposter-blocker-thumbnail
    $9

    AdPoster Blocker

     (2)
    A simple but powerful plugin that blocks spammy new user regs and/or ads/jobs/posts.
  • StarStruck WordPress Plugin Thumbnail
    $19

    StarStruck

     (14)
    A fast, lightweight, and elegant star rating system for comments, pages, & posts.
  • GeoReg thumbnail
    $29

    GeoReg

     (10)
    Captures detailed geographic info with new user registrations.
  • stripe-plugin
    $39

    Stripe

     (4)
    Process credit cards safely and securely on your AppThemes website.
  • autoauto-thumbnail
    $39

    AutoResponder AutoRegistration

     (4)
    Automatically integrates WordPress registration with major autoresponders.
  • featured
    $9

    QR Codes Widget

     (3)
    Add a widget with QR codes to your AppThemes site.
  • daddy-likes-190x130
    $19

    Daddy Like

     (10)
    A fast, lightweight, and elegant "like" system for comments, pages, posts, & activities.
  • Authorize.Net Thumbnail
    $39

    Authorize.Net

     (3)
    Easily start accepting online payments via credit card and e-check.