Dynamic page caching / excluding parts from caching

I use W3 Total Cache to cache WordPress pages, posts, etc.
Now I made a button that sets the session variable to tell the server if the user wants to see the mobile or desktop and calls this function:

<?php function mobile_css() { if(is_mobile() && !isset($_SESSION['mobile'])) : ?> <link type="text/css" media="all" rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/mobile.css" /> <?php elseif($_SESSION['mobile'] == "ja") : ?> <link type="text/css" media="all" rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/mobile.css" /> <?php endif; } 

The problem is that the pages get cached every time they load. So, if I open the page with the mobile session, it will show the mobile phone, if I go to the desktop and pick up this page again, it will still show the mobile version, because it was cached, so the first time it was requested.

Does anyone have an idea on how to prevent this?

+4
source share
1 answer

Well, the solution was fragment caching . First you need to set the passphrase by setting

 define('W3TC_DYNAMIC_SECURITY', 'my_string'); 

in wp-config.php and then you can use php code like this

 <!-- mfunc echo "Hello World<br/>"; --><!-- /mfunc --> 

which outputs this dynamic code

 <?php echo "Hello World<br/>"; ?> 
+6
source

Source: https://habr.com/ru/post/1494032/


All Articles