Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Website theme switcher by URL

0
by DamsFX, last modified on October 3rd, 2023

This page, if duplicated across themes, allows you to cycle between them. It is possible to limit the themes available for this loop (see comment note).

Take care : This affects the whole website (as it override the site config), not just the current user visiting the page. If you want a more powerfull solution take a look at : Hounddd.ThemeSwitcher plugin.

title = "Theme switch"
url = "/switch-theme"
layout = "default"
is_hidden = 0
==
<?php
    function onStart()
    {
        use Cms\Classes\Theme;
        use Redirect;

        $themes = collect(array_pluck(Theme::all(), 'dirName'));
        /**
         * Use the above syntax to limit the availaibles themes
         * to those specified
         */
        /*
        $themes = collect([
            'first-theme-code',
            'other-theme-code',
        ]);
        */

        $currentThemeKey = $themes->search(Theme::getActiveThemeCode());

        $nextThemeKey = $currentThemeKey +1;
        if ($nextThemeKey > $themes->count() -1) {
            $nextThemeKey = 0;
        }

        $nextTheme = $themes->only($nextThemeKey)->first();
        Theme::setActiveTheme($nextTheme);

        return Redirect::to('/');
    }
?>
==

If only two themes are defined, the page behaves like a "theme switcher".

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?