I am moving from parent theme to child theme on an ecommerce website. When I move to the child theme, the parent themes configrations are removed and the site's design starts looking ugly. In order restore the parent theme configuration, the theme_mods_theme_name row in wp_options table needs to be changed to theme_mods_child_theme_name and before that the entry for theme_mods_child_theme_name which was created when moving to child theme needs to be deleted. Doing these changes on the live site directly is dangerous. How can I write a script that that does the configuration change automatically? Thanks


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.

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.