8

My client uses Karma theme with many settings in theme options. I need to keep my CSS and PHP additions in a child theme so that theme updates don't wipe it out. However when I select my child theme in Appearance > Themes, the main theme settings are all gone.

Is there any way to preserve the main theme settings?

  • what do you mean in "gone"? If you can switch back to the parent theme and the setting are there then they are not gone – Mark Kaplun Oct 5 '14 at 5:26
  • Gone from the dashboard and website, that is. They still exist somewhere in the database though but that's not relevant to my problem. – drake035 Oct 5 '14 at 11:09
  • so what is your problem? you asked "Is there any way to preserve the main theme settings?" and since they are still in the DB they are preserved – Mark Kaplun Oct 5 '14 at 13:13
  • "Preserved" meaning present in child theme. When switching from parent to child theme I want child settings to be the same as parent settings. – drake035 Oct 5 '14 at 14:09
  • then copy them. Have to admit I still don't get what exactly is the problem you are trying to solve. sound like you are trying to complicate you life, or your approach of doing a child theme is wrong and you better modify the parent theme (or it might be that the parent theme is not suitable to be used for child themes) – Mark Kaplun Oct 5 '14 at 15:53
8

Because of the way these theme settings are stored as an array in the database, it can be difficult to copy them over with just copy and paste in phpmyadmin or some similar tactic.

The WP CLI option command is your friend here. If you don't use WP CLI already, check it out! Here's how I copied the settings from the storefront theme to a theme called storefront-sqcdy-child:

# save the existing theme settings in json format in a temporary text file
wp option get theme_mods_storefront --format=json > theme_mods_storefront.txt

# load the saved settings into the child theme option record in the database
wp option update theme_mods_storefront-sqcdy-child --format=json < theme_mods_storefront.txt

# cleanup the temp file
rm -f theme_mods_storefront.txt
  • As second line you should use a pipe too wp option update theme_mods_storefront-sqcdy-child --format=json < theme_mods_storefront.txt – Gregor Mar 3 at 20:15
  • Yes, I suppose that's a more standard way to do it... – squarecandy Mar 3 at 20:32
6

Wordpress theme modifications are saved in wp_options database table in theme_mods_{themename} field. You can copy it and rename using your child theme name.

  • 1
    And when the parent theme updates and adds new options, what do you do then? – Mark Kaplun Mar 31 '15 at 7:34
  • What is a better option @MarkKaplun? I think this solves the problem the user is asking – Eoin Nov 24 '17 at 0:16
  • 1
    @Eoin the more questions I read about child themes, the less I like the limitations of it. It seems like for any non trivial change in the parent theme you will have to reevaluate the child theme code. If all you do is CSS, there is little problem, but for anything else reevaluation is needed. the OP here will need to make sure he syncs the options with the parent theme on each upgrade (in case a new option with non empty default value was added). I find myself advocating against using child themes, and instead use git to branch the "parent" and make merges when there are updates. – Mark Kaplun Nov 24 '17 at 3:26
  • When you say, rename using the child theme's name, what specific name do you mean? I dont understand where to get the child theme name from. – Fahad Uddin Apr 30 '18 at 14:12
0

You could do something like what Genesis child themes include to save default theme settings when activating and switching themes:

//* Theme Setting Defaults
add_filter( 'genesis_theme_settings_defaults', 'child_theme_defaults' );
function child_theme_defaults( $defaults ) {

    $defaults['blog_cat_num']              = 6;
    $defaults['content_archive']           = 'full';
    $defaults['content_archive_limit']     = 150;
    $defaults['content_archive_thumbnail'] = 0;
    $defaults['image_alignment']           = '';
    $defaults['image_size']                = 'entry-image';
    $defaults['posts_nav']                 = 'prev-next';
    $defaults['site_layout']               = 'full-width-content';

    return $defaults;

}

//* Theme Setup
add_action( 'after_switch_theme', 'child_theme_setting_defaults' );
function child_theme_setting_defaults() {

    if( function_exists( 'genesis_update_settings' ) ) {

        genesis_update_settings( array(
            'blog_cat_num'              => 6,
            'content_archive'           => 'full',
            'content_archive_limit'     => 150,
            'content_archive_thumbnail' => 0,
            'image_alignment'           => '',
            'image_size'                => 'entry-image',
            'posts_nav'                 => 'prev-next',
            'site_layout'               => 'full-width-content',
        ) );

    } else {

        _genesis_update_settings( array(
            'blog_cat_num'              => 6,
            'content_archive'           => 'full',
            'content_archive_limit'     => 150,
            'content_archive_thumbnail' => 0,
            'image_alignment'           => '',
            'image_size'                => 'entry-image',
            'posts_nav'                 => 'prev-next',
            'site_layout'               => 'full-width-content',
        ) );

Clearly you would need to modify this code to work with your themes functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.