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 5 – Set Child Theme Default Options at Theme Activation

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

Applies to -

The first thing we’re going to do is create a new PHP file and it’s going to be crtc_setup_theme_options and while we’re at it, we’ll come back over here and add a new include without our setup_theme_options file. Now that we’ve included this file, come back over to our setup_theme_options and I’m going to paste the function I’ve already written.

Set Default Options Function

Now, we’re going to talk about this function here that sets our default options. The first thing that it does is it calls this global variable called all_options. This all_options variable is set when a child theme is activated and it’s set by the core, set by WordPress. We have this and this all_options variable contains all of the options in the database. This just gives us access to all of the options that are in the database.

The next thing we’re going to do is create a variable which is the name of the file. What we’re doing here is, CRTC_PATH, this is a constant that we defined at the very beginning which with get_stylesheet directory so CRTC_PATH represents the root of our child theme. Once you’re in the child theme root, we’re going to call the name of our file. I actually call our file thesis-responsive-options.dat. What we have right now then is the local address of this file for our function to use.

Create PHP Function for the File

Now that we have the name of the location of this file, we’re going to check first to make sure that the file exists. So we’ve written this whole address to a variable called file, now we’re using a PHP function called is_file. That PHP function returns either true or false so what you do is put the name of a file in here and it tests to see if that file exists, that’s all it does.

If that file exists, then we’ll keep going, if it doesn’t, then what we’re going to do is print this error that says, “Whoops, that dat file can’t be found”. It’s just an error message that comes up if that file doesn’t exist.

The first thing we do is check to make sure the file exists, if the file exists, then we use another PHP function called file_get_contents which can take more than one attribute. In this case, we’re going to only use a single attribute and that is the name of the file.

There are other attributes you can specify as well such as where to start inside the file and where to stop inside the file but our file is created without any extra characters at the beginning or at the end so we don’t really have to worry about offsetting the content that we’re getting. All we’re doing here is file_get_contents($file) and that’s essentially taking the value of that dat file that we look at and it’s placing all of those characters in this variable called ($raw_options).

Create the Structure of a Variable Using a Thesis Class

Then we’re going to use a Thesis class called thesis_design_options to create the structure of a variable that we’re calling design_options and again, it doesn’t matter what we call this, it’s just a variable name. In this variable name, we are creating a structure out of this thesis_design_options class. What we’re going to do next once we have that structure, is unserialize that variable. We’ve got our ($raw_options) that takes all of that odd looking content which is a serialized array and unserialize it.

Unserialize Data

Now, serialization in this case is being used to safely and efficiently store the information in that .dat file. That .dat file was created with the serialized comment so in order for us to make that data useful to WordPress, the first thing we have to do is unserialize that data. We’re using the PHP function unserialize which takes the contents of that variable, unserializes it and places it in this variable called design_options.

If you’ve used any caching with Thesis, you know that W3 Total Cache has a bit of difficulty with Thesis options in particular. This is a function that checks to see if W3 Total Cache exists. If it exists, what we do is use a function inside of W3 Total Cache that essentially says, delete the cache for thesis_options or in fact, delete the options cache, period. It’s going to have to recreate that but what we wanted to do is clean the cache first then we use this command called update_option.

WordPress Options API

What I want to talk about is the WordPress options API. This is a safe method of storing information in the database. It means that you don’t have to create your own database tables and you don’t have to worry about writing directly to the database or reading directly from the database. It takes this database function and it adds options to that database and to WP_Options Table inside of the database.

WP_Options Table

The WP_Options Table is designed and intended to be the storage place for all theme and plugin options and we’re adding options to this in just a moment. I want you to see that this has add_option and add_site_option. The difference between option and site option is that the option relates to a specific WordPress installation. Site option refers to a multisite installation and it applies to all of the sites within that site.

If you’ve got a multisite installed and you have 20 different sites inside of that multisite installation, then add_option would only apply to whichever specific site was using it and applies that option to all sites. We’re not working in multisite right now so we’re just using add_option.

You also notice that we’ve got add, delete, get and update. It’s fairly obvious what each of these things does. You’re either adding, deleting, reading – that’s getting and updating. However, update stands in quite nicely for add_option because you’d use add_option if the option doesn’t exist but what you can really do is simply use update_option which will create the option if the option doesn’t exist.

If it does exist, then it will replace the contents of that option with whatever else you tell it to do. For example, update_option looks like this, then the name of the option you want to update and the new value that you want to place inside of that option. It’s quite easy to write an option to the Options Table and to retrieve those options without having to deal with the database itself.

Updating the Option

If we come back over to our code, what we’re doing here is we are updating option, thesis_design_options, that’s the name of the option that’s being updated. In the place of the value of thesis_design_options, we are placing the value of this variable, design_options.

The update_option is not additive, we’re not adding this design_options to the existing thesis_design_options. What update_option does is it entirely replaces the content of this with the value of this. When you are updating an option, if you’ve got an option that has lots of values in it, you have to update all of those values.

If you only try to update a single option, what it does is it erases all the other values and only saves that single option. So, update_option replaces all of the value of this option with the value of this variable with whatever the option happens to be.

Thesis Generate CSS

Then what we do is call this Thesis function called thesis_generate_css. What this does is it goes to the thesis_design_options and reads them, it writes the CSS based on those design options and generates that layout.css file. That’s what we want to have happened as soon as we update these options.

Theoretically, what will happen is, when we activate our theme, we’re not going to have to go to Thesis Design Options and hit save or update because we’ve included the command for it to do that as soon as the update option is completed.

This is our functions, crtc_set_default_options, it tests to make sure that options file exists. If the file exists, then it gets the content of that file, puts it in this variable called, $raw_options, creates a new variable called design_options and gives it a Thesis structure.

It takes that structure variable and inserts this unserialized version of the $raw_options, it cleans out the W3 Total Cache if you’re using that and if you’re not using it, it doesn’t matter then it updates the thesis_design_options with this new design_options and writes the Thesis CSS. Fairly straightforward.

What we’re going to do now is copy the name of that, save it. We have to come back over to our function here, we’ve got our theme_activate hook. Now, inside of that, we need to place a call to our function. Upon theme activation, what’s going to happen is, this function is going to be called. That function reads our default options and writes those default options in the WordPress database. Let’s test that.

Reactivate the Default Theme

First, we need to upload, we go to admin and we’ll add. So, we’ve added those files, now we need to make sure that we also add our updated_functions.php file and our thesis_responsive_options.dat.

Let’s come over to our site and we’re going to reactivate the default theme. Thesis 1.8.4 is now set up. What I want to do is go back to Manage Options, erase all those design options and restore the design options. There we go, we’re all the way back to default Thesis.

Now, what we’re going to do is activate our child theme and if our code works, what will happen is, our design options will be changed. If we take a look at our design options, there’s two columns instead of the 3. If we refresh this, perfect, we have all of our options. Your user can go from a blank installation of Thesis and install your child theme and automatically, all of your child theme settings are set.

However, what happens now is if your user goes back to the standard Thesis and looks at the site, a whole bunch of those design options still say the same. If your user had a nicely designed site and just wants to see what your child theme might look like your user is going to be sorely disappointed to discover that you have screwed up his design options. That’s what we’re going to fix next.

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