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.

Add New Comment
Viewing 12 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)
8/25/2006 at 11:33 pm
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 ...
10/3/2006 at 1:00 am
[...] Elliott Back [...]
10/22/2006 at 10:13 am
[...] Getting WP-Cache to Work [...]
10/24/2006 at 12:22 am
[...] Finally, what if you want to use both WP-Cache and gzip? Here’s how. [...]
11/6/2007 at 5:27 am
[...] WP Super Cache 0.2 is out! I think all the bugs mentioned below are now fixed. I applied Tummbler’s ...