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 6 – Play Nice, Restore Original Options at Theme Deactivation

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

Applies to -

We’re going to make our child theme so that it restores the original design options. Actually, we’re going to test this, we’re going to go back to Manage Options, we’re going to Restore Default Design Options then we come over to Design Options and change something. Let’s make the Primary link text color red. Now, here’s our user’s customized design options with red text and we’re going to restore this back to his original thing after we deactivate our child theme.

Create Function to Save Default Options

We have this function that sets the default options. Now, we are going to create a function that saves the default options. Here we are, crtc_save_original_options, we create a variable called, $existing_options and we use the get_option function to get the thesis_design_options. We take thesis_design_options and we stored in $existing_options then we use our update_option and we are essentially creating a thesis_responsive_original_theme_design_options.

We could say, add_option and then this but that’s not necessary here, we’re just going to say, update_option and we are updating this new option with the old option values. That’s one part of it. We need to take this function, crtc_save_original_options and before set_default_options, we’re going to say, save_original_options. Here at theme activation, we’re going to save those original options and set the default options.

Restore Original Options at Theme Deactivation

At theme deactivation, we want to restore the original options. I’m sure you can imagine what this looks like. We have created a variable called $original_options and we’re going to get this option that we created here, get the value of that option and store it in this then we’re going to use the very same thing, update_option and update the thesis_design_options with the original_options.

We are playing nicely and this is going to happen at theme deactivation. Copy this, come back over to our functions.php file, paste it. So at theme deactivation, we’re going to restore our original_options. This is essentially the entire system, we save our original_options and we set our default_options at theme activate.

At theme deactivate, we restore the original options. Actually, I think there’s one more thing we should do here and that is delete this original_options option and that happens right here. We’ll say delete_option and what we’re going to delete is the thesis_responsive_original_theme_design_options.

Actually, what we should really do here is name this crtc_original_theme_design_options so that it’s clear to everybody who sees this that this is associated with the child theme. There’s a mistake on my part with this. Really, we should preface all of these options that we create with our crtc which is our preface to all of our functions.

So, delete_option(‘crtc_original_theme_design_options’) because once you  have deactivated the theme, you no longer need to store all these in that table. It could very well be that somebody wants to just test your child theme but never has any intention of ever using it or they make take it off and never use it again.

Delete and Setup Theme Options

The problem is that if we don’t delete this option then, we make them store that in their WordPress installation forever so we need a mechanism to essentially be a good neighbor. When somebody uses them, we’re going to do our best to preserve the integrity of what they have already created and restore it back to that condition then we’re picking up after ourselves and we’re not leaving a mess in the database that they don’t need. That’s what’s happening here, let’s just test that, save both of those.

We’re going to upload functions, go to Admin and upload, setup theme options then come back over to our website and Appearance and Themes, we’re going to activate that. Having activated it, we have all of our theme options setup so that is working properly. Now, what we’ll do is go back and deactivate it. We’ll activate this and if this is working properly, what happens now is when we refresh this, we get our red text. Those original theme options were saved and restored.

I don’t know of any child theme that does this. This is something that I believe is important so I’m teaching you how to do it but I haven’t seen any child theme actually do this primarily because the theme activation hooks don’t exist.

I’m guessing because the theme activation hooks don’t exist and because there’s no accepted way of doing this that child theme designers have chosen not to bother with it but I think the most mature way to handle this kind of thing is to leave no trace. When you install your child theme, it cleans up after itself and if somebody deactivates their child theme and deletes their child theme, there’s no trace of that child theme left with the original theme.

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 Comments… add one
0 comments… add one

Leave a Comment