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.4 - 12 votes

Popular Add-ons

Mapsupreme for Vantage

A plugin that adds additional Google Maps to your Vantage theme.


(28)
$29

WP Email Styler

Add good looking html template to emails


(3)
$19

AutoResponder AutoRegistration

Automatically integrates WordPress registration with major autoresponders.


(5)
$39