Domanda

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?

È stato utile?

Soluzione

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/>"; ?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top