Getting WP-Cache to Work
WP-Cache is a caching / load balancing plugin for wordpress that takes dynamic pages and staticizes them. It’s based of Matt’s Staticize and Staticize Reloaded series of plugins, and allows you to include dynamic content in the cached template with a special “matt include” command. However, getting this to work with a plugin is hard, especially if you want to get the post id or other information while running your plugin in wordpress. Here’s what I did to make it work:
- Let’s query the database. Since I use the $wpdb variable/class directly, I need to have it around when my plugin runs. Normally, wp-settings.php would have included it, but since WP-Cache is higher in the chain of command, you need to manually require it: require_once (ABSPATH . ‘wp-includes/wp-db.php’);
- Where are my WP functions? With WP-Cache, many of your favorite included functions are now gone. So, instead of using wordpress’s get_option function to keep track of the creation of my databse, I had to set up a constant manual check: if(!WP_FROM_WHERE_SKIP_DB_CHECK){…}
- Who am I? All of the sudden, the_ID() doesn’t work, and I can’t figure out the post ID. So, I created a special function to get the post identifier:
function wp_from_where_id(){
global $post, $posts, $meta_obj, $comment_post_ID;$post->post_id;
if(empty($id)) $id = $post->ID;
if(empty($id)) $id = $comment_post_ID;
if(empty($id)) $id = $posts[0]->post_id;
if(empty($id)) $id = $posts[0]->ID;
if(empty($id)) $id = $_GET['p'];
if(empty($id)) $id = $meta_obj->post;return $id;
}This special function makes a modification to wp-cache-phase-1.php to enable its magic. I renamed the meta variable to $meta_obj and made it global: global $meta_obj; in the top section. That way, when the meta information is unserialized:
$meta_obj = new CacheMeta;
if (! ($meta_obj = unserialize(@file_get_contents($meta_pathname))) )
return;it contains the post_id under the name “post,” which we can extract as a last-resort. With WP-Cache on, we need that.
- The weird syntax. Yes, to call a plugin dynamically in wp-cache you need to do the following:
<!–mclude wp-content/plugins/wp-from-where.php–>
<?php //include_once(ABSPATH . ‘/wp-content/plugins/wp-from-where.php’); ?>
<!–/mclude–><!–mfunc wp_from_where(true, ‘DESC’, 20, ‘<li>’, ‘</li>’, ”)–>
wp_from_where(true, ‘DESC’, 20, ‘<li>’, ‘</li>’, ”)
<!–/mfunc–> - No gzipping. It’s very disappointing that WP-Cache doesn’t use gzip by default. Just add the following function somewhere in wp-cache-phase1.php:
function gzip_accepted() {
global $HTTP_ACCEPT_ENCODING;
if (strpos($HTTP_ACCEPT_ENCODING, ‘gzip’) === false) return false;
if (strpos($HTTP_ACCEPT_ENCODING, ‘x-gzip’) === false) {
$encoding = ‘gzip’;
} else {
$encoding = ‘x-gzip’;
}return $encoding;
}Then, all you need to do is replace the regular “load and output file” section with a section that tests if the client can handle gzip compression or not:
// GZIP
$encoding = gzip_accepted();
if (!$encoding){
// Try to get cached file
if ($meta_obj->dynamic) {
include($cache_file);
} else {
header("Content-Size: $content_size");
if(!@readfile ($cache_file))
return;
}
} else {
// Try to get cached file
if ($meta_obj->dynamic) {
ob_start();
include($cache_file);
$contents = ob_get_contents();
ob_end_clean();
} else {
$contents = file_get_contents($cache_file);
}$gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
$size = strlen($contents);
$crc = crc32($contents);
$gzdata .= gzcompress($contents, 9);
$gzdata = substr($gzdata, 0, strlen($gzdata) - 4); // fix crc bug
$gzdata .= pack("V",$crc) . pack("V", $size);
$gzsize = strlen($gzdata);Header(’Content-Encoding: ‘ . $encoding);
Header(’Vary: Accept-Encoding’);
Header(’Content-Length: ‘ . strlen($gzdata));
Header(’X-Content-Encoded-By: class.gzip_encode ‘.$this->_version);echo $gzdata;
}This gzip code is derived from leknor.com/code/php/class.gzip_encode.0.62.php.txt which is LGPL.
Those were the major hurdles I overcame to enable my new plugin to run on this blog. Phew!
| This entry was posted on Tuesday, July 12th, 2005 at 1:44 am and is tagged with return id, obj, chain of command, phase 1, dynamic content, wordpress, meta, wp, magic, manual check. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback. |
17 Responses to “Getting WP-Cache to Work”
Leave a Reply

How did you get your search to function dynamically? I have also implemented WP_Cache on my site, but for the life of me I cant figure out how to get my search function to work. Any tips would be greatly appreciated!
If youre feeling really helpful, this is the code for my search that no longer works
My Search
To be honest, Nick, I can’t really tell you why it doesn’t work since I can’t find a search box on your homepage!! Most likely its a javascript bug?
Hmm, not sure either. But yeah, I took the search box off of the mainpage. Im just not sure how to use the mclude thing. Aw well, Ill keep poking around. Thanks.
I put the search function back on my site, if you could check it out. I made a page called search.php with my search form on there. Then I called it like this
<!–mclude /search.php–>
<?php include_once(ABSPATH . ‘/search.php’); ?>
<!–/mclude–>
But, it just gives me this single page with the word I searched for…
OK, Nick. Here’s exactly how the search works on my site. In the folder wp-content/themes/greenmarinee/sidebar.php, there’s a chunk that reads:
<h3>The Search</h3>
<p class="searchinfo">search site archives</p>
<div id="search">
<div id="search_area">
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input class="searchfield" type="text" name="s" id="s" value="" title="Enter keyword to search" />
<input class="submit" type="submit" value="" title="Click to search archives" />
</form>
</div>
</div>
As you can see, all it does is call the wordpress engine with an “s” parameter–and it’s the default search of the template. WP-Cache will then cache the results page that WP spits out.
Hello Elliott.
I have been trying to work out how to make my dynamic plugins work with wp-cache.
One of the plugins in question is this adrotator (from: http://blog.taragana.com/index.php/archive/wordpress-plugin-adrotator-rotate-your-ads-including-adsense-dynamically/).
I made a file called: tbcode.php
With this in it (that’s the code used to call the adrotator plugin normally):
And then put this in my template:
Note the tbcode.php file is on the base directory.
It seems to work the first time fine (it produces the advert banner) but upon refresh it does not work at all.
I get this error:
Fatal error: Call to undefined function: getad() in /home/XXXXXX/public_html/tbcode.php on line 1
Any help or pointers would be apprecited (feel freet to email).
Thanks. Francis.
Do you know how can i make the sidebar or widgets to load dynamically with wp-cache? I have a hard time trying to let my online counter work again …
Okay, I’m an id10t, I’ll admit. But I’m looking at my wp-cache files, trying to figure out what the heck the “the regular ‘load and output file’” section is so I can replace it with the suggested code, above.
I’m at sea, here. Exactly what portion of WP-cache code do I replace?
Rich.
BlogRodent
Parches para WP-Cache…
WP-Cache 2.0 es un sistema de cache de páginas realizado por Ricardo Galli (con algo de testeo y documentación por mí parte en los inicios
) imprescindible para aliviar la carga del servidor y los tiempos de carga de los blogs basados en WordPres……
My site got slashdotted last night and I had to enable the cache plugin to make it load again. I have troubles understanding one concept and I thought maybe you could help me understand it.
What is happening to adsense scripts when they are cached ? Is the javascript cached or the output of the javascript ? The first would mean that people would see wrong ads while the latter would mean everything is running as usual.
It would be nice if you could answer the question
regards
Martin
[...] Elliott Back [...]
[...] Getting WP-Cache to Work [...]
[...] Finally, what if you want to use both WP-Cache and gzip? Here’s how. [...]
I’ve been trying to get wp-cache switched on and yet give me dynamic postviews and recentcomments… I tried using your code (changing the path and plugin files) and inserted it in my theme file… but am getting errors probably because the plugins themselves are looking for other functions in files the wp-cache prevents from reaching (speculating as am not a programmer).
For post views to work I included the following code as you mentioned it:
It error-ed out giving a message that it was could not find the function name in the postviews.php
Stuck.
Wasn’t clear where to include/modify in what files the other pieces of code you have mentioned before the calling plugin code.
[...] WP Super Cache 0.2 is out! I think all the bugs mentioned below are now fixed. I applied Tummbler’s patch (from Elliott and Reiner) that enables gzip compression of the WP-Cache data files and fixes feed content types. [...]
I think your syntax/format is not correct with mclude. I was able to successfully get it to work by using the following:
with NO slash before ‘wp-content’ for the first path.
hii there i receive this message occasionally on my blog..
i have de activate the plugin..but this error still came about..do you know why it’s still looking for this file..i believe tehre are still a few lines in my wp-settings.php asking for advanced-cache.php and i still have advanced-cache.php on my blog..
how do i remove them without affecting my blog?
Warning: require(/home/syokkahw/public_html/blog/wp-content/advanced-cache.php) [function.require]: failed to open stream: No such file or directory in /home/syokkahw/public_html/blog/wp-settings.php on line 69
Fatal error: require() [function.require]: Failed opening required ‘/home/syokkahw/public_html/blog/wp-content/advanced-cache.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/syokkahw/public_html/blog/wp-settings.php on line 69