17

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

9
+50

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 );
  • Your first point of concerned is not really justified: if the current theme is not a child theme then get_template will simply return the current theme's dir name. You don't need to know the relation between the previously active theme and the current one: if the theme has a parent it loads those settings otherwise it just loads its own settings. I agree though with your second point which addresses the issue of settings being overwritten. – webtoure Aug 10 '15 at 6:31
  • Furthermore, what if the user accidentally activates another theme before activating the desired child theme? The code will not work and the (theme_switched, current_theme) relation is irrelevant to begin with. – webtoure Aug 10 '15 at 7:04
  • 1
    @MarjanVenema I'll be very happy if you'll give bounty to webtoure. – gmazzap Aug 10 '15 at 12:51
  • 1
    @MarjanVenema gmazzap actually provided comments for his thought process and also suggested a correction to my snippet. I think if anybody deserves the bounty, it's him. Also, I don't answer questions for completely useless points. I love: interacting with people, challenges and generally helping when I can. – webtoure Aug 10 '15 at 13:29
  • 1
    Please take extended discussions to chat, comments are not meant for them. :) – Rarst Aug 10 '15 at 15:44

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.