Theme Options: Filtering How Your Options Are Saved

From the previous article, you know that the entire options page is saved as one big array to the standard options table of the WordPress database.

$settings = get_option('jumpstart');

But what determines the ID your options page is actually saved to in WordPress? — Our framework takes the name of the currently active theme, makes it lowercase, and removes all spaces and special characters.

So, for example, if you weren’t using a child theme, and Jump Start was the currently active theme, the options ID would be jumpstart.

Using a Child Theme

Now obviously, if you’re reading this article and making customizations to the theme options page, unless you’re creating a custom plugin, you’re using a child theme.

So, what happens when you switch to a child theme? — This is where people get tripped up. If you’re using a child theme, the name of the active theme is no longer Jump Start (or whatever Theme Blvd theme you’re using). But instead, it’s the name you’ve set in our child theme’s style.css.

And, if you’re using the default child theme that came with your theme as a starting point, you may not have thought to change the name of it, which is called Your Child Theme. Thus, your theme options are currently saving with to the ID yourchildtheme.

$settings = get_option('yourchildtheme');

Creating a Custom Value For Saving the Options

Changing the value for the options to be saved to is easy. Simply filter in a custom string, using the filter themeblvd_option_id.

function my_option_id( $id ) {
    return 'my-custom-id';
add_filter('themeblvd_option_id', 'my_option_id');

Now all settings are saved in your database in a way they could be retrieved like this:

$settings = get_option('my-custom-id');

Switching to a Child Theme and Losing Your Settings

Did you switch to a child theme, and lose all your settings? — Fear not. This is one of the most common issues people have when switching to a child theme after already setting up their website. And unfortunately, it tends to scare a lot of people away from using a child theme for the first time, but don’t let it!

Depending on when you originally downloaded the sample child theme for your theme, you may see something like this within the functions.php:

 * Maintain options ID for saved options from parent
 * theme. (optional)
 * This allows you to switch between parent and child theme,
 * with your theme options remaining saved to the same value
 * in your WordPress database.
function themeblvd_default_option_id( $id ) {
    return 'jumpstart'; // usually matches get_template()
add_filter('themeblvd_option_id', 'themeblvd_default_option_id');

This just allows you switch freely between parent and child theme with any name, and have all saved settings from the theme options page remain in place.

So, if you ran into this problem of losing your settings, simply add the snippet above to your child theme’s functions.php (replacing jumpstart with the ID of your parent theme, if you’re not using Jump Start). And if you switched a child theme after previously already setting up your theme options and didn’t run into the issue, you probably already have the snippet above in your child theme.

Note: Because so many people get spooked by installing a child theme for the first time and thinking they’ve lost their saved theme options — In 2016, we started implementing the above modification directly into the sample child themes included with all themes.