How do I make my child theme re-apply the settings that were customised when its parent was active?

Scenario

  1. Pick 2015 theme. Customize various settings, such as background picture, header picture, site name and tagline.
  2. But don’t touch the contents of any of the theme’s files.
  3. Use Codex’ Child Themes to create a bare minimum child theme of the 2015 theme.
  4. Activate the new child theme.

Oops?

The site with this bare minimum child theme activated looks completely different from the parent theme. Background and header image were gone, and all manner of sidebar (or header) widgets had been activated that were not active (by default) in the parent 2015 theme.

Original look of the site with just a few customised settings for the 2015 theme:

SofietjeWonders with customised 2015 theme

The site as it looks with the 2015 theme derived child theme, which has just the required header in style.css and only enqueues the styles as suggested by the Child Theme page of the Codex in its function.php.

enter image description here

Question

How do I make it so that activating the child theme keeps the customisations and defaults of the parent theme?

In this case: the header and background pictures and the removal (deactivation?) of the default (sidebar/header) widgets.


Notes

Create a child theme of an already heavy customised theme may sound like a duplicate, but that is about customisations by editing the theme’s files.

This question When activating a child theme, what core settings have to be reset ie; Custom Menus etc? seem to ask exactly what I seem to be after. Unfortunately, the only answer speculates about a mismatch in features, which is not the case for me and didn’t pick up on the OP’s comment to the same effect.

I also looked at

  • When activating a child theme, what core settings have to be reset ie; Custom Menus etc?. It is related, but it doesn’t answer my question.
  • This:
    Register theme customizer settings when theme activates [duplicate] also seems related, but doesn’t address what I want.
  • Nor does the question it was marked a duplicate of: Settings API enable default settings on theme install? which is about saving the child theme’s settings, not about re-using or re-applying the parent theme’s settings for the child.

1

I gave a +1 to the @webtoure’s answer because it gives you the right direction, however I think it lacks some checks.

First of all it does not check that the theme that’s being activated is a child theme, and does not check that the theme previously active is the parent theme of the child theme being activated.

Per my understanding of OP these conditions are required.

Moreover, one issue that you need to take into account is what to do with the theme mods of the child theme being activated, if they already exist.

In @webtoure answer, they are stored in a backup, that might save you under some circumstances, but WordPress will not recognize them by default and so they require some additional code to be used.

I think it would be better, to inherit the theme modifications from parent theme only the first time a child theme is activated.

In short, the conditions I want to check before to inherit theme mods from parent theme are:

  • the previously active theme must be the parent of the child theme that is being activated
  • the child theme that is being activated has to never been activated before

To ensure the second condition I’ll use a custom option, because WordPress does not provide a way to do this check.

This is the code, please read inline comments for explanation of what is going on:

add_action( 'switch_theme', function( $new_name, \WP_Theme $new_theme ) {

    // get the previously active theme
    $previous = get_option( 'theme_switched', -1 );

    // get the parent of current theme, will be false if no parent
    $parent = $new_theme->parent() ? $new_theme->get_template() : false;

    // current stylesheet name
    $stylesheet = get_option( 'stylesheet' );

    // has the theme being activated ever been activated before?
    $lastActive = get_option( $stylesheet . '_last_active', false );

    // if previouly active theme is the parent of the the child theme being activated
    // and it has never been activated before..
    if ( ! $lastActive && $parent === $previous ) {

        // update "last_active" option so following code won't run again for this theme
        update_option( $stylesheet . '_last_active', current_time( 'timestamp', 1 ) );

        // get the theme mods of the parent
        $previousMods = get_option( 'theme_mods_' . $parent, array() );

        // inherit current theme mods from parent theme mods
        update_option( 'theme_mods_' . $stylesheet, $previousMods );
    }

}, 10, 2 );

Leave a Comment