Set active theme via URL

时间:2017-11-30 作者:ProfK

我有一个新的可湿性粉剂与几个候选主题网站,并希望最终用户能够“浏览”我的主题。为此,我想使用Theme Switcher Ryan Boren的插件,或者更好的插件(如果有的话),因为有些东西让我相信它可以让我指定一个通过URL激活的主题。但是,我找不到任何关于如何将其配置为URL解析的文档。

我所能找到的只是一个主题切换器边栏小部件,如果最终用户(甚至我)使用该小部件切换到没有边栏的主题,就会产生问题。

我也不想把它作为结尾。我想为客户端提供一个URL列表,每个URL都针对同一个站点,但具有不同的主题参数。

PS: 我找到了一些通过帖子URL为帖子选择模板的解决方案,但我不确定帖子类型模板是否与完整的已安装主题相同。

1 个回复
SO网友:kierzniak

我创建了一个简单的类,它将完全满足您的需要。若要切换主题,必须使用主题的名称将主题获取参数添加到站点url。http://example.com/?theme=twentyfifteen

选定的主题也将存储在cookie中,因此用户下次进入站点时,他将拥有先前选择的主题。

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();

结束

相关推荐

Virtual Pages plugins

我很难让插件正常工作Virtual Pages (WordPress插件可简化虚拟页面的创建)我确实进行了编辑,根据查询创建了一个循环。add_action( \'gm_virtual_pages\', function( $controller ) { /* Creating virtuals pages for companies */ $args = array( \'post_type\' => array(\'companies\',), \'post_status\'