2

I have a new WP site with a couple of candidate themes, and would like the end user to be able to "browse" my themes. For that, I would like to use the Theme Switcher plugin By Ryan Boren, or something better if available, because something led me to believe it would allow me to specify a theme to be activated via the URL. I cannot find any docs on how to configure it for URL parsing though.

All I can find is a Theme Switcher sidebar widget, which causes trouble if the end user (or even me) uses that widget to switch to a theme with no sidebar.

I don't want that as an end feature either. I want to present the client with a list of URLs, each for the same site but with a different theme parameter.

PS: I have found a few solutions that select a template for a post via the post's URL, but I'm not certain whether a post type template would be the same as a full on, installed, theme.

  • have you checked how the preview theme works? – David Lee Dec 2 '17 at 6:01
  • @DavidLee No, it didn't at first occur to me to do that. In fact, I'm busy looking at it now. Thanks. – ProfK Dec 2 '17 at 8:26
  • The shareable preview link, with the customize_changeset_uuid GET parameter, from the Customizer, came first to mind here, but unfortunately it doesn't support theme switching, see e.g. tickets #42411 and #42126 – birgire Dec 2 '17 at 9:41
5
+100

I created simple class which will do exactly what you want. To switch theme you must add theme GET parameter to your site url with name of the theme. http://example.com/?theme=twentyfifteen

Selected theme will also be stored in cookie so next time the user enters the site he will have earlier selected theme.

class WPSE_287356_Theme_Switcher {

    /**
     * Theme which we want to activate
     */
    private $theme;

    /**
     * Cookie name
     */
    private $cookie;

    /**
     * GET parameter name
     */
    private $get;

    /**
     * Switched
     */
    private $switched;

    /**
     * Class constructor
     */
    public function __construct() {

        /**
         * Get default theme
         */
        $this->theme = get_option('template');

        /**
         * Define cookie name
         */
        $this->cookie = 'theme';

        /**
         * Define get parameter name
         */
        $this->get = 'theme';

        /**
         * Define if theme is already switched
         */
        $this->switched = false;

        $this->handle_url();
        $this->switch_theme();

        /**
         * Define plugin related hooks
         */
        $this->define_hooks();
    }

    /**
     * Get theme
     */
    public function get_theme() {

        return $this->theme;
    }

    /**
     * Handle for theme change
     *
     * When the user clicks special link with theme name we will
     * store this theme name in user cookies and 
     */
    public function handle_url() {

        if( isset( $_GET[$this->get] ) && !empty( $_GET[$this->get] ) ) {

            $theme = filter_input(INPUT_GET, $this->get, FILTER_SANITIZE_STRING );

            // This part require some additional checking e.g if theme exits or is allowed.

            $this->theme = $theme;
            $this->switched = true;

            // Store theme in cookie to remember choice
            setcookie( $this->cookie, $theme, time() + ( 365 * DAY_IN_SECONDS ), COOKIEPATH, COOKIE_DOMAIN );
        }
    }

    /**
     * Switch theme
     *
     * Check if user has cookie with theme name and eventualy switch theme
     */
    public function switch_theme() {

        if( isset( $_COOKIE[ $this->cookie ] ) && !$this->switched ) {

            $theme = filter_input( INPUT_COOKIE, $this->cookie, FILTER_SANITIZE_STRING );

            // This part require some additional checking e.g if theme exits or is allowed.

            $this->theme = $theme;
        }
    }

    /**
     * Define plugin related hooks
     */
    private function define_hooks() {

        /**
         * This filters will replace theme name across all WordPress
         */
        add_filter( 'template', array( $this, 'get_theme' ) );
        add_filter( 'stylesheet', array( $this, 'get_theme' ) );
    }
}

new WPSE_287356_Theme_Switcher();
  • I see the filters are set in the ctor, but where do I place this class and how do I trigger it's instantiation, in this case? – ProfK Dec 3 '17 at 7:28
  • You can place it in your plugin or theme main file depending what you are doing. This script is working on each request, there is no need for installation. – kierzniak Dec 3 '17 at 11:22
  • 1
    It will have to be a plugin, because the whole objective is to change themes. If I put the code in any theme, I'll have to put it in all available themes. – ProfK Dec 4 '17 at 5:43
  • 2
    So you have to make a plug-in. – kierzniak Dec 4 '17 at 7:15
  • I awarded the bounty because of your effort, and because it looks like it will work, but I will accept after a test which I can't do now because my PC is on the fritz. – ProfK Dec 9 '17 at 7:44

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.