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
1 Answer
Reset to default 0Try 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
版权声明:本文标题:themes - change template with button 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744719050a2621583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论