admin管理员组

文章数量:1391999

I created plugin with code :

function fxn($theme) {

if(isset($_POST['zmien'])){
    $theme = 'loose';}
else{
    $theme = 'twentytwenty';}

return $theme;
}

add_filter('template', 'fxn');
add_filter('option_template', 'fxn');
add_filter('option_stylesheet', 'fxn');

I found the code and adapted it to my website. I have created the plugin and it works only partially. When I click the button the template changes but when I click on any element on the new template the page returns to the previous template.

How can I make a change of template active until the end of browsing the page?

I created plugin with code :

function fxn($theme) {

if(isset($_POST['zmien'])){
    $theme = 'loose';}
else{
    $theme = 'twentytwenty';}

return $theme;
}

add_filter('template', 'fxn');
add_filter('option_template', 'fxn');
add_filter('option_stylesheet', 'fxn');

I found the code and adapted it to my website. I have created the plugin and it works only partially. When I click the button the template changes but when I click on any element on the new template the page returns to the previous template.

How can I make a change of template active until the end of browsing the page?

Share Improve this question asked Feb 24, 2020 at 15:43 spokspok 72 bronze badges 9
  • 1 What are you trying to implement? Is it a theme switcher in the admin area? A demo preview? Different solutions may be applicable depending on what you're trying to build, any context you can add would be super helpful – Tom J Nowell Commented Feb 24, 2020 at 20:30
  • The code provided in my answer would work for a single user browsing the site, how then wants to change the theme. If you are trying to do something else you will have to explain more, as Tom J Nowell mentioned above. – RiddleMeThis Commented Feb 24, 2020 at 20:35
  • I must have two versions of the site. One ordinary, the other in a completely simplified version for people with restrictions. I thought that there would be a switch from the basic template to the latter and that it would be available for the user of the page. And this solution is only appropriate in this situation. None of the existing plugins do not. – spok Commented Feb 24, 2020 at 21:40
  • You can make a minimal version of your site with just CSS, you can do a lot with it. This will be the recommend approach rather than switching the theme. – RiddleMeThis Commented Feb 24, 2020 at 22:05
  • so make a second css for the template and customize it to a minimum? But how to load it later so that it changes? – spok Commented Feb 24, 2020 at 22:11
 |  Show 4 more comments

1 Answer 1

Reset to default 0

Try using switch_theme().

<?php switch_theme( $stylesheet ) ?>

I have personally never used it but it sounds like what you want. You just need to pass the stylesheet name you are using.

Here is a very basic demo I put together. You would basically but the following code, where ever its needed in both themes.

<form action="" method="post">
    <input type="radio" id="theme1" name="theme" value="theme1">
    <label for="theme1">Theme1</label><br>
    <input type="radio" id="theme2" name="theme" value="theme2">
    <label for="theme2">Theme2</label><br>
    <input type="submit" value="Submit">
</form>

<?php
    $radioVal = $_POST["theme"];

    if($radioVal == "theme1"){
        switch_theme('gemcore-ui');
        header("Refresh:0");
    }
    else if ($radioVal == "theme2"){
        switch_theme('testtheme');
    }
?>

I tested this and it worked well but I needed to refresh the page afterwards, so I added the header("Refresh:0"). Also you can add as many radio options as needed.

本文标签: themeschange template with button