Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

WordPress Cache PluginsI have been using W3 Total Cache and WP Super Cache for years. They have both been really solid performing plugins that work well. If you are not familiar with either of these what they basically do is create static html based files in your hosting account. The first time someone visits a page or post that has not been cached yet php does the processing, and the static file is created. The next time someone visits they get the cached html file which helps reduce server power and resources, and it will allow your site to handle more traffic.

Sounds great right? Usually for most sites.

The problem I am dealing with on another website that I am working on is showing different content to visitors based on whether or not they have visited before.

If they are a new visitor I want to show them a short video that automatically starts playing on the main homepage. If they are a repeat visitor the video won’t automatically play, they need to click the play button.

Sounds simple enough right? Wrong.

Not when using page caching with W3 Total Cache or WP Super Cache. That’s because once the static html based cache file is initially created that’s what everyone will get until it expires or is cleared out. So, if a new visitor happens to be the one that generated the static cache page with the video automatically playing returning visitors will also get a video that starts playing. And, if a returning visitor happens to be the one generating the cache page with the video that does not auto play then, new visitors will also get a cached page with a video that doesn’t auto play.

So what are the options then?

Good question. This has been a difficult one to get working, which gave me a major headache.

Headache

Option 1 is to turn off page cache completely, which really isn’t an option for me. However, it would allow php to do the processing instead each time, and the visitor would get the right video.

Option 2 turn off page cache for the page the video happens to be on. This would allow php to handle the processing as well so the right video would be displayed too. Since this happens to be on the homepage, which is the most popular page it’s not really an option either.

Option 3 add define(‘DONOTCACHEPAGE’, true); to the php file that I want to disable cache on. In my case this resulted in disabling cache on the entire homepage again, which wasn’t what I was after.

Option 4 use fragmented caching, which is supposed to allow you to exclude specific lines of php from being cached. The documentation on this feature is pretty sparse, and I never did get it to work right. One minute it would, and the next it wouldn’t. So after spending more time with it than I should have I moved on to option 5 below.

For additional information about using fragmented caching with W3 Total Cache you can check out the following links:
W3 Total Cache Fragment Caching in WordPress
Implementing Page Fragment Caching in W3 Total Cache
WordPress.org support forums

You are supposed to be able to do something similar with Super Cache as well, but the information available is even harder to find than what’s out there for W3 Total Cache.

Side Note:
W3 Total Cache has an option to turn off page cache for a specific page. I have seen people suggest that you can add specific php files such as header.php, footer.php, sidebar.php, etc. to the “Never cache the following pages:” option in the page cache settings. However, I don’t believe this feature works this way. It’s for disabling cache on a specific page or post that was created in the WordPress administration dashboard. You would need to use fragmented caching above in option 4, or some other method instead like myself if this is what you are trying to accomplish.

For example the image/screen shot in this reply showing sidebar-4.php and sidebar-5.php wouldn’t work:
Best way to show dynamic content on a cached WordPress site

It is more for individual posts and pages like this:
W3TC Never Cache Pages
Option 5 is to have new visitors get the cached static html page with the video that automatically starts playing, and to essentially disable cache for returning visitors. That way php will do the processing and allow them to have a non-auto playing video.

Basically new visitors get a cached page and returning visitors don’t. Not a perfect solution, but some caching is better than none.

The way I have it configured is if it’s a first time visitor a 30 day cookie is set. Then, to display the right video I check to see if the cookie exists. If it does exist show them video A, if it doesn’t show them video B.

Simple enough right? Wrong again!

That’s because I want to check and set the cookie with php. What happens is the new visitor is getting an already cached html based file and php can no longer check or set the cookie for the first time visitors.

So now what?

Option 1 was/is to use javascript/jquery/ajax to check and set the cookie. I never did get this to work the way I wanted, but it’s an option.

Option 2 was to create an external non-WordPress php page that is not included anywhere in my theme files. Then, load it into a DIV ID=”SOMETHING” using jquery. That way the homepage is still cached for the first time visitors, and the external php page is just checking and setting a 30 day cookie if necessary.

The bottom line is if you don’t want a specific WordPress php theme file cached if you are using W3 Total Cache or WP Super Cache, then you will have to do some serious thinking and testing.

I spent way more time getting this to work than I should have, but that comes with developing and programming.

About: Jeremy LeSarge - AKA: Ray (211 Posts)

I am the site owner and administrator of DialMe.com. I provide help and tips for Boonex Dolphin on the main part of this website where you will also find an assortment of other resources. Here, on the blog I write about a variety of topics surrounding WordPress, technology, social media/networking, SEO, and webmaster resources.