Moving from parent theme to child theme without losing configurations

Obvious solution: don’t switch on the live site. You could use a staging environment instead, but this can become tricky with ecommerce sites if orders have been placed in the meantime. Alternatively shut down your site for a couple of minutes.


If this is not feasable, there may be other ways. To understand, how theme switching works, check out the switch_themes( $stylesheet ) function in wp-includes/theme.php. This is the function that manages transition from one theme to another.

I see many ways that you could inject your custom code in there somewhere, you’ll have to test which way fits your needs best.

  1. Listen to update_option( 'theme_switch_menu_locations', ... change. This is probably one of the first ways you could inject custom code in. However, here you do not really know the the new template

  2. Listen to update_option( 'stylesheet', $stylesheet ), here you know the new theme ($stylesheet) and can get the currently active one via wp_get_theme(). The theme hasn’t switched completely yet, so you can still create the new options as you see fit

  3. Either listen to update_option( 'theme_switched', $old_theme->get_stylesheet() ) or to do_action( 'switch_theme', $new_name, $new_theme, $old_theme ). The theme switch for most parts has already happened, default values will have been set and visitors in the meantime will see your “ugly” site.

I would probably go for solution 2. (ok, false, I wouldn’t do these changes on a live site). The new options have not been checked/set yet, so you can copy what you need from the old template and prepare these values. So when WordPress checks for the settings, you’ll already have them pre-filled.

tech