WP Super Cache for Developers

WP Super Cache is a full page caching plugin for WordPress that can dramatically speed up blogs.

The plugin is very flexible and can be configured or modified with the aid of filters, actions and the configuration file to handle many situations.

The plugin operates in three modes. Mod_rewrite, PHP and LEGACY. In the first two modes static cache files are created in the supercache cache folder (usually wp-content/cache/supercache/). No PHP is executed when these files are served by Mod_rewrite. PHP mode serves the same supercached files but loads a tiny amount of PHP code to do so and is very quick. In LEGACY mode, the cache files are created in the specified cache folder (usually wp-content/cache/) using a filename created from an md5 of the current url, gzip encoding status and the user’s cookies. The mobile user agent may also be added if that’s switched on. The filename may finally be filtered using the “wp_cache_key” filter as explained below.

Configuration

The configuration file is WP_CONTENT_DIR/wp-cache-config.php (usually wp-content/wp-cache-config.php). The configuration is stored in a file because the plugin loads before the database library is loaded. You can modify or add to the configuration file through the following command,

wp_cache_replace_line($old, $new, $my_file)

Parameters:$old is a regular expression matching an existing line in the file.$new is the new line to replace or be inserted.$my_file is the actual file.
Example:

Plugins

WP Super Cache has it’s own plugins folder. It needs this to make it easier to extend as it loads before regular WordPress plugins. These plugins are loaded before the database library, or almost all the regular WordPress include files are loaded. They won’t have access to WordPress features like $wpdb, add_action() or add_filter() when they load.

The plugin folder normally lives in wp-super-cache/plugins/ but by modifying the $wp_cache_plugins_dir variable in the config file you can put it anywhere. That’s a good idea if you develop your own supercache plugins because WordPress will delete the folder wp-super-cache and everything in it when upgrading the plugin.

Plugins can interact with the early initialisation process using the “cacheaction” functions. add_cacheaction() and do_cacheaction() work exactly like their WordPress equivalents.

add_cacheaction( $action, $func )

Parameters:$action is the hook to add an action on.$func is the function to run when the specified hook or action is executed.

Cacheaction hooks

The following are the cacheaction hooks already defined in WP Super Cache and brief description.

wp_cache_key filters the cache key used to index half-on cache files.

wp_cache_served_cache_file filters the filename of the half-on cache file for the current url.

wp_cache_get_cookies_values filters the cookie string identifying the current visitor.

cache_admin_page allows you to add new content to the WP Super Cache admin page. Use this in your own plugin to add an on/off switch for it on the admin page. See the existing plugins for an example.

add_cacheaction is fired after regular actions are added in wp-cache-phase2.php. Use this cacheaction to execute a function that adds new actions to regular WordPress actions or filters.

Moving things around

You can tell the plugin where various important directories sit:

Define WPCACHEHOME in your wp-config.php to point at where the wp-cache-phase1.php and associated files live. This is usually wp-content/plugins/wp-super-cache/

Point $wp_cache_plugins_dir are where you want the WP Super Cache plugins folder to live. See “Plugins” section above.

Set $cache_path to where the cache directory should live. This is set in your configuration file.

Late Initialisation

If you’re writing a WordPress plugin that needs to modify the cache key used by WP Super Cache but also needs access to the database use this feature.

Set $wp_super_cache_late_init to 1 in your cache config file.

This will cause the plugin to delay setting the cache key until the “init” hook in WordPress executes. It has a really low priority of 9999 so it’s likely to run after any other hooks. This may cause problems with plugins that use this hook to start an output buffer. Consider yourself warned!
The plugin will also enter half-on mode. Now you can modify the cache key using a hook on wp_cache_key and interrogate the database too.

Garbage Collection

Garbage collection is the process of cleaning up stale or obsolete cache files. The garbage collection(GC) process fires off the wp_cache_gc scheduled event in WordPress. This fires off an action of the same that runs the function wp_cache_gc_cron().

If for some reason the GC process doesn’t run, you can set $wp_cache_shutdown_gc to 1 in the config file so it will execute at the end of each request instead. (It won’t do an actual cleaning up of cache files on each request.) This will result in a slow down so it’s better to fix your wp-cron system instead.

Clearing the Cache

You can clear the cache by calling the function wp_cache_clear_cache().

Delete the cache files for a single post by using the wp_cache_post_change( $post_id ) function. It will attempt to delete every half-on cache file for that post, as well as any supercache files.
You may need to set the global variable $super_cache_enabled to make this function clean out supercached files. In requests that have GET parameters supercache is disabled so only wp-cache cache files are created, and likewise deleted. Use this code to set that variable before your call the post change function:

$GLOBALS["super_cache_enabled"]=1;

Filters

It’s possible to filter a number of useful items during the caching process:

do_createsupercache takes the cookie values as a parameter and decides if supercache files should be created or not. Probably of limited use.

supercache_dir filters the supercache directory. 1 parameter, the supercache directory above the directory “supercache”. For example, this page would be “ocaoimh.ie/wp-super-cache-developers”.

wpsupercache_404 returns false by default but have it return true to cache 404 pages.

wpsupercache_buffer filters the content of the page. If your plugin uses an output buffer you could use this filter instead. (Yes, this could be super useful!)

wp_super_cache_error_checking will allow you to add extra error checking before the plugin settings page loads. Your filter should print a warning message and return false to stop the settings page loading.

I think the most useful filter there could be wpsupercache_buffer. With it, you can examine and modify the page before it’s served and cached. Any plugin that depends on an output buffer could use this instead (and avoid any potential ob conflicts!)
The three mobile filters are only useful if you have mobile support switched on. Please see wp_cache_check_mobile() and wp_cache_mobile_group() in wp-cache-phase1.php for how they work.

Do not cache this page

Define the constant DONOTCACHEPAGE at any time before the current request finishes and the current page won’t be cached.

The Golden Compass
First of a three part fantasy/sci-fi series. Some people hate it because of it's anti God message but it's a great read. I found it hard to put down. There's even a Snopes article about the film adaptation.