Build Your Own Business Website header image

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

Lesson 8 – Part 3 – Create Theme Activation/Deactivation Hooks

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

Applies to -

Now, that sounds great and you say to yourself, “Well, so let’s do that for theme activation”. Unfortunately, now that I’ve told you how to create a hook in WordPress like this, I have to tell you that you can’t create a theme activation hook like this.

Issues With Creating Theme Activation Hooks

The difference between the theme activation hook and this hook that we just created is that for the hook that we just created, we have access to the core files. We are creating the core files so we are creating this file inside of our child theme and since we have access to it, we can put the placeholder there. However, we don’t have access really to WordPress core files.

Of course, you could hack the WordPress core file but that would be insane and it also would mean you have to hack WordPress every time you want to use this child theme so that’s, of course, not an option. Since you don’t have access to the core, you can’t put a hook at the theme activation location or before a theme is activated. What you have to do is essentially fake that.

Now, if you look around the web, you’re going to find that there are a bunch of different suggestions on how to do this but really, I think there’s really only one of them that’s of any real benefit. It’s the tutorial that is created by Khrishna Kant Sharma and you can probably find this on my Facebook page and on my Google+ page because I have links to it in those places.

The link to this article will be in the lesson resources when that gets posted later this week. However, if you search for activation/deactivation hook for WordPress theme, you’ll find this article by Khrishna Kant Sharma. His article includes this PHP Library and this is what we are going to use in order to create our theme activation and deactivation hooks.

This is one of the beautiful things about working with open source is that, everybody and their brother can work on it, can come up with a concept and as a general rule these things are often very actively shared. This is just one of those brilliant examples of why this kind of thing works really nicely. I am going to copy all this because this is what we’re going to use.

Create Theme Activation/Deactivation Hooks

We should get the whole thing down there and copy it then we’re going to create ourselves a brand new file. We’re going to come over to our Admin section – actually, we haven’t created any Admin files yet unless I’m in the wrong spot. No, I’m in the right spot so we’ll create a new file and we’re going to call this, crtc_activation_deactivation_hooks and we’re just going to replace this code with Khrishna’s code then I’m going to show you what it does.

Essentially, his code is designed to be run every time the theme is loaded which is every time a page is rendered. When this theme is activated, the first thing it does is it comes and looks at this function and if there is this option in the options table that says, “This current theme is activated”, then it doesn’t do anything. If that option does not exist, then it knows that this is being run the very first time after the theme has been activated and it calls a function that we’re going to create that is our activation hook. It adds the option saying that the theme is now been activated.

Essentially, this function is only going to result in an action after the theme has been activated but before anything else happens. That’s when this thing is going to fire and do something, otherwise, it’s fired once after activation and it never does anything else ever again.

Switch Theme Hooks

The second thing that it looks at is the reverse. What happens is, at this WordPress action called “switch_theme”, it fires this function. It doesn’t do anything unless the “switch_theme” action is fired and when the “switch_theme” action is fired, it fires this function and this function follows the deactivation code that we’ll be creating in a moment then delete this option that was created up here.

What we’re doing is, we’re using the options table to store a switch that if it doesn’t exist and this is checked, then our hook is executed and the switch is turned on which means that hook never happens again until this option doesn’t exist anymore. The reverse happens as well when this hook “switch_theme” happens then it will execute our deactivation hook functions and it will delete the options that’s created here.

Include the File In Our Functions

So we’ve created our file, now we need to include this file in our functions. If we come over here to functions and again we use include_once. Let’s see, we haven’t created an Admin path yet so what we’re going to do is define a new constant crtc_admin, the folder is admin so there’s our new constant.

Here, we’re going to create a new include and include the admin and the file name that we’re including is crtc_activation_deactivation_hooks. We’ve created a new constant and we’ve included our new file with that constant. That’s the second step. First step was to create the file itself, second step is to include it.

Create an Activation Function

The third step is to create an activation function. I have an activation function written. Here’s our theme_activate and we may as well create our theme_deactivate as well. What we’re doing here every time the theme loads is we are calling wp_register_theme_activation_hook and when we call that, we call it with the name of our child theme which in this case is Thesis Responsive and with the name of the function that we want to execute which is crtc_theme_activate.

The same thing is true here, it’s not the theme_activation_hook. What it is, is the wp_register_theme_deactivation_hook. That’s the one we’re going to call next and it’s crtc_theme_deactivate. So you’ve got the library and in order to make the library work, you need to create two functions.

You need to create a deactivation function and an activation function. It doesn’t matter what you call them, but we’re calling them activate and deactivate just so that it’s clear what we’re talking about. Each of those functions needs to be called referencing one of these two functions. So the activation hook, it’s referenced with the wp_register_theme_activation_hook so here’s that function call.

Function Attributes

That function call includes two attributes, it includes the name of your child theme and includes the name of the function that’s being called which is this theme_activate. The same thing happens with the deactivate, we call this register_theme_deactivation_hook. This works with regular themes too but it takes the name of the child theme and it takes the name of the function that we’ve created that we want to run a deactivation.

Right now, neither of these functions is doing anything because they’re empty. The reason I’m explaining this to you is because if you look at the comments down here, nobody seems to be able to figure out how to actually do this. I think it’s probably a combination of an engineer talking to a regular person and an English translation problem because his directions aren’t particularly clear. Having said that, I trust my directions are clear.

The critical part of that is calling his function with the name of your theme and the name of the function you want to call at activation. And calling his deactivate function with the name of your theme and the name of the function you want to act on when the theme is deactivated. We’ve just now created these two hooks that allow us to hook into these.

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
THIS IS A LIMITED TIME OFFER!
Subscription Options
0 Comments… add one
0 comments… add one

Leave a Comment