Question

I am using W3 Total Cache for caching my WordPress pages, posts, etc.
Now I made a button that sets a session variable to tell the server if the user wants mobile or desktop view 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 pages get cached every first time they get loaded. So, if I open up the page with session mobile it shows mobile, if I switch to desktop then and visit that page again it still shows mobile-version because it was cached so the first time it got requested.

Does anybody have an idea of how to prevent this?

Was it helpful?

Solution

Ok, the solution was fragment caching. First you have to set your secret phrase 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/>"; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top