Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm implementing a site that allows users to switch stylesheets (to show a high contrast version for people with vision impairments). The switcher essentially just changes the <link>ref to whichever stylesheet is appropriate.

However, I've just realised that this will lead to problems when I switch on caching (either using WP Supercache or W3 Total Cache) - as only one version of the page will be cached and thus displayed to the user.

Any thoughts on possible solutions?

share|improve this question
    
Can you commit to specific caching plugin or you want more generic solution that will work with any? –  Rarst Jul 6 '11 at 16:26
    
@rarst - I don't mind which one is used, so yes, I can commit to a specific plugin. –  anu Jul 6 '11 at 16:37
add comment

3 Answers

I use W3TC, so these are approaches that I can come up with it for it:

  1. Fragment caching to exclude that part of page, will reduce cache effectiveness overall.

  2. Identify page with switched stylesheet by query argument, disable caching of such pages.

  3. Identify page with switched stylesheet by URL endpoint, disable caching of such by mask.

  4. Implement stylesheet switch via JavaScript (not really W3TC-specific).

share|improve this answer
    
The problem with (2) and (3) is that the switch is a global thing - so I don't think that will work, looks like (1) and (4) are the contenders. –  anu Jul 7 '11 at 8:13
add comment

Idea: If you enqueue the styles, use filemtime() for the version. If it finds changes to your previous stylesheet, caching will be prevented and a new stylesheet gets loaded.

share|improve this answer
    
FYI, you linked to the german version of the PHP page for filemtime. –  Mark Duncan Jul 6 '11 at 19:08
    
ups! ;) Thanks for the heads up. Changed... –  kaiser Jul 7 '11 at 0:10
    
Damn. Haven't seen, that @Rarst already changed it... Mr. Rarst: Feel free to roll back! –  kaiser Jul 7 '11 at 0:11
1  
@kaiser - interesting, I'll try this approach out. –  anu Jul 7 '11 at 8:14
add comment
up vote 2 down vote accepted

Turns out the answer is fairly simple (at least for WP Supercache).

  1. Use either PHP or legacy caching (ie not mod_rewrite)
  2. Turn on late init (either from the UI - recommended, or by setting $wp_super_cache_late_init = 1; in wp-content/wp-cache-config.php
  3. Use the <!--dynamic-cached-content--> directive to wrap the content that should remain dynamic.

An example:

<!--dynamic-cached-content--><?php
    display_high_contrast_link();
?><!--
   display_high_contrast_link();
--><!--/dynamic-cached-content-->

As you can see, the dynamic content has been added twice - from the WP Supercache FAQ

The first code is executed when the page is cached, while the second chunk of code is executed when the cached page is served to the next visitor.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.