0

I have a question. I am trying to make a theme compatible with another one. For example if you have this theme named "old theme" and you change it to "new theme", options from customizer will remain in "new theme" also. Everything works great if I go in Appearance -> Themes and switch themes from there. The problem comes when I switch theme from customizer. I'm using something like this (let's say I want to import a text control) :

function import_settings{ 
  $old_theme_content = get_option('theme_mods_my_old_theme');
  if( !empty($old_theme_content['my_text_control']) ){
    set_theme_mod('my_new_control', $old_theme_content['my_text_control']);
  }
}
add_action( 'after_switch_theme','import_settings');

When I switch the theme from customizer, I have to close the customizer, eventually remove the changeset and after that I can see those changes from import. I want the changes to be visible in customize preview after I switch the theme from there. It seems that 'after_switch_theme' hook doesn't trigger then. Does anyone have any idea how to achieve this?

0

Well, you can read the core code to find the actions, or you can use a debugging method to find the one you need.

Put into wp-config.php:

define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );

Then make a mu-plugin file containing:

<?php 
add_action( 'all', '_action_printer' );
function _action_printer( $a ){
    if (strpos('gettext', $a) === false && strpos('escape', $a) === false) {
      $x = func_get_args();
      $x = print_r($x, true);
      error_log( $x .  "\n", 3, dirname(__FILE__). '/../debug.log');
    }

}

This will output the arguments to all the actions to the debug.log file in the wp-content folder, so it will get big fast and slow down your site. (I removed two since they are used a lot.) Use it just long enough to capture the actions for what you need, which is switching themes in the Customizer. Or just print the action name ($a) instead for a smaller output.

Once you have the list of actions, you can amend your code to hook to the appropriate action. You may have to check is_customize_preview() to make sure the code is run at the right time. Be aware that the user can preview lots of themes without actually choosing to use one, so your theme really should not be writing to the database when in the Customizer (because it's just a preview).

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.