16

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?

7
  • 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 Commented Oct 5, 2014 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
    Commented Oct 5, 2014 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 Commented Oct 5, 2014 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
    Commented Oct 5, 2014 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) Commented Oct 5, 2014 at 15:53

6 Answers 6

18

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
7
  • As second line you should use a pipe too wp option update theme_mods_storefront-sqcdy-child --format=json < theme_mods_storefront.txt
    – Gregor
    Commented Mar 3, 2019 at 20:15
  • Yes, I suppose that's a more standard way to do it... Commented Mar 3, 2019 at 20:32
  • 1
    Hi squarecandy, you said "it can be difficult to copy them over with just copy and paste", but the operation you perform seems to me the same. Am I wrong? Commented Oct 24, 2019 at 7:37
  • 1
    I'm quite sure that with your commands get [update] to [from] a file, your are doing the same thing Commented Oct 25, 2019 at 7:32
  • 2
    See wpengine.com/support/wordpress-serialized-data - the "Potential Conflicts" section for why you can't just copy over the serialized value and expect it to work 100% of the time. WP CLI takes care of all of this for you. Commented Oct 25, 2019 at 17:25
11

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.

6
  • 1
    And when the parent theme updates and adds new options, what do you do then? Commented Mar 31, 2015 at 7:34
  • 1
    What is a better option @MarkKaplun? I think this solves the problem the user is asking
    – Eoin
    Commented Nov 24, 2017 at 0:16
  • 2
    @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. Commented Nov 24, 2017 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.
    – user2238
    Commented Apr 30, 2018 at 14:12
  • @fuddin the theme name is the name of the folder of the child theme; for example: parent theme settings are identified by the option_name 'theme_mods_parent-theme-name'; child theme settings could be identified by the option_name 'theme_mods_parent-theme-name-child'. You can perform a select on your database in order to view that clearly: SELECT * FROM wp_options WHERE option_name LIKE "theme_mods_%" Commented Oct 24, 2019 at 7:43
3

I know this is an old question, but I needed to do this and it was quite simple:

NOTE: This is mostly useful for themes that use the WordPress Customizer to configure their settings

  • Take a backup of your website (files and database) using a plugin such as Updraftplus
  • Get access to your database whether via Cpanel's phpAdmin or alike
  • Go to your {prefix}_options (wp_options) table and run the query:

SELECT * FROM {prefix}_options where option_name like 'theme_mods_%'

This will pull up the theme mods saved for all themes.

  • Look for the option with the name of your current active theme example: theme_mods_mytheme

  • Copy it's option_value and save to a text file or something.

  • Go back to WordPress and activate your new child theme

  • Then go back to the database and refresh the options/run query again and you should see a new option now called theme_mods_mytheme-child (assuming your child theme is called 'mytheme-child')

  • Edit it's option_value, delete what's there and paste the previously copied data that you saved to the text file and save the changes.

  • Go back to the site and refresh the page, you'll see that things now are the same as it was with the parent theme.


This method also works if you have a child theme folder named something like 'mytheme-child' and you maybe want to rename the folder name to 'newtheme' after you've already done Customizer changes to the site while 'mytheme-child' was active.

2

This plugin worked very well for me

Child Theme Copy Settings

1

I solved by simply copying the options in the database.

Here are the step-by-step procedure:

  1. Log into your phpMyAdmin and select the database of the webiste
  2. Backup the database
  3. Execute this query in order to have a clear view of the situation: SELECT * FROM wp_options WHERE option_name LIKE "theme_mods_%"; this instruction will return a row for every theme you have activated on your website so far
  4. Modify the row of the parent theme settings, identified by option_name = theme_mods_parent-theme-name
  5. Here, in the option_value field, you have all the parent theme settings in a serialized form; copy them; you can use an unserialize tool in order to see the content in a human-friendly form
  6. Come back to the point 3, but now modify the row of the child theme settings
  7. Paste the settings you have copied before in the option_value field; save
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 and acknowledge you have read our privacy policy.

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