• 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 Switcher

    When 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/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Ugh… I really didn’t want to filter options, but it seems there are leftover places that don’t use wrapper and no decision which way should it be adjusted, see https://core.trac.www.ads-software.com/ticket/20027

    The issue is I can’t quickly fix this simply by switching to filtering options, since there are some naaaasty edge cases – like craziness with multiple theme directories registered which I recently hit and really not happy with temporary solution.

    Since the ticket probably won’t produce results till WP 3.6, what I will probably work on throwing out some code that makes things complicated (theme list and fields retrieval) and switch to filtering options when I can toggle it to either more easily.

    Cool, thanks for the link and feedback.

    The issue is I can’t quickly fix this simply by switching to filtering options, since there are some naaaasty edge cases – like craziness with multiple theme directories registered which I recently hit and really not happy with temporary solution.

    Edge cases indeed! Not to mention the admin (and customizer) end of things. Since first posting, I’ve run into some problems with the little workaround above. I think I’ll need to do something similar to template, as I did with stylesheet. I noticed that background images set in Twenty Eleven are not being overridden. (It’s working for my theme though, which uses a custom callback for the background image). I’m still working away at it, but I’ll report back with any improvements.

    Just chiming in with the latest revision of the workaround I came up with. It fixes the issue with the background image always being the one set in the real theme, and not the one from the previewed theme. This isn’t pretty, but it works (did not test multi site or multiple theme dirs).

    /**
     * Filter value of stylesheet to fix issue with wrong background image being shown
     * on theme selected by TTS. I think the problem is rooted in how get_background_image()
     * (and related code) work, but I was not able to completely wrap my head around the issue.
     *
     */
    add_filter( 'pre_option_stylesheet', 'tts_force_stylesheet' );
    function tts_force_stylesheet( $option ) {
    
    	if ( ! class_exists( 'Toolbar_Theme_Switcher' ) )
    		return $option;
    
    	// Bail if we're in the customizer
    	global $wp_customize;
    	if ( isset( $wp_customize ) )
    		return $option;
    
    	remove_filter( 'pre_option_stylesheet', 'tts_force_stylesheet' );
    
    	// Check if we are using a child theme
    	if ( get_template_directory() !== get_stylesheet_directory() ) { // (It's too early for is_child_theme().)
    		// This doesn't seem like it should work, but it does. https://en.wikipedia.org/wiki/Mr._Magoo
    		// $option will be set to the human readable theme name. e.g. Twenty Eleven Jr.
    		$option = Toolbar_Theme_Switcher::$theme_name;
    
    		// This will set $option to the theme dir name, which I would think would be
    		// what we want, but it does not work. e.g. twentyelevenjr
    		// $themes = Toolbar_Theme_Switcher::get_allowed_themes();
    		// if ( isset( $themes[Toolbar_Theme_Switcher::$theme_name]->stylesheet ) )
    		//	$option =  $themes[Toolbar_Theme_Switcher::$theme_name]->stylesheet;
    	}
    	else {
    		$option = Toolbar_Theme_Switcher::get_theme_field( 'Stylesheet' );
    	}
    
    	add_filter( 'pre_option_stylesheet', 'tts_force_stylesheet' );
    
    	return $option;
    }

    I pushed bunch of changes to the development repository https://bitbucket.org/Rarst/toolbar-theme-switcher/raw/tip/toolbar-theme-switcher.php

    Reaaally dislike option filtering, but so far it seems to work properly with customizer. I will poke it some more before releasing new version, let me know if you test and have any issues with this one.

    Cool! I’ve gave it a whirl, and now the background images are working as expected. I no longer need to use that code I posted before.

    I noticed that when I used the theme switcher with my theme, for some reason the menus and widgets dissapear.

    I traced through wp_nav_menu() with a debugger and found that it passed through get_theme_mod, used the get_option(‘stylesheet’) and seemed to break down there because it was not using the name of the switched theme, but the “Real theme” (as defined above).

    I swapped in you’re dev version and it worked.

    @m4olivei

    Thanks for feedback! Please let me know if you experience any issues with dev version. I want to do couple more things and then will release update.

    Had been running dev version for a while so released that as version 1.2. Seems to work robustly with everything that I have tried.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Issue with get_theme_mod and Toolbar Theme Switcher’ is closed to new replies.