Issue with get_theme_mod and Toolbar Theme Switcher
-
While using Toolbar Theme Switcher, I noticed that the wrong background was being displayed when I was previewing a theme.
Since it can be confusing, here is terminology I’ll use:
Real theme: Theme actually set in Admin > Appearance > Themes (i.e. Theme that’s set without the use of the Toolbar Theme Switcher plugin.)
Previewed theme: Theme selected via Toolbar Theme SwitcherWhen viewing the previewed theme, get_theme_mod() is returning values for the real theme instead of the previewed theme. I traced the function calls for get_background_image(), and came up with this:
get_background_image()
get_theme_mod()
get_theme_mods()
$theme_slug = get_option( ‘stylesheet’ );So, the issue is that $theme_slug is being initialized to the value for the real theme.
I used the following code to force WP to use the previewed theme’s $theme_name using the pre_option_{option_name} filter:
(I used this pattern, because it seemed like the best way to use this powerful filter.function tts_force_stylesheet( $option ) { if ( ! class_exists( 'Toolbar_Theme_Switcher' ) ) return $option; remove_filter( 'pre_option_stylesheet', 'tts_force_stylesheet' ); // do what you like with $option $option = Toolbar_Theme_Switcher::$theme_name; add_filter( 'pre_option_stylesheet', 'tts_force_stylesheet' ); return $option; } add_filter( 'pre_option_stylesheet', 'tts_force_stylesheet' );
https://www.ads-software.com/extend/plugins/toolbar-theme-switcher/
- The topic ‘Issue with get_theme_mod and Toolbar Theme Switcher’ is closed to new replies.