Switching gallery / attachment image srcs to a static domain / CDN in WordPress

For the majority of sites that I manage, I use a www. subdomain as the main URL, which dynamic pages are served from. For static assets, such as images, javascript, CSS, etc. I use a static subdomain. This makes it easy to send long expires headers for everything on the static subdomain, and keep cookies restricted to the www subdomain only, resulting in a faster website.

Normally I write my posts in HTML, meaning I explicitly set the src of any images included in the post to point to the static subdomain. However, I realised today that on one of my sites I’m using the [gallery] shortcode. With this WordPress automatically creates an image gallery based on the images added as attachments to the post, and of course, will just use the standard www. subdomain for the image srcs.

Thankfully, you can filter the src that WordPress uses for attachment images very easily. In my theme’s functions.php, I added the following:

add_filter('wp_get_attachment_image_src', 'staticize_attachment_src', null, 4);
/*
* @param array|false  $image         Either array with src, width & height, icon src, or false.
* @param int          $attachment_id Image attachment ID.
* @param string|array $size          Registered image size to retrieve the source for or a flat
*                                    array of height and width dimensions. Default 'thumbnail'.
* @param bool         $icon          Whether the image should be treated as an icon. Default false.
*/
function staticize_attachment_src($image, $attachment_id, $size, $icon)
{
	if (is_array($image) && !empty($image[0])) {
		$image[0] = str_replace('http://www.', 'http://static1.', $image[0]);
	}
	return $image;
}

You need to hook into the wp_get_attachment_image_src action. The callback has 4 paramaters – the first is the important one in our case, $image. This is a numerically indexed array, with index 0 being the src of the image. So you can just do a find and replace on the src in the callback to replace the default subdomain with your static subdomain.

Bear in mind that if your site uses / allows HTTPS, then you’ll need to modify the code above appropriately.

Posted on by xoogu, last updated

Moving the WP Super Cache Plugins folder

WP Super Cache is a plugin for WordPress that allows you to speed up your site by making use of server side caching. The plugin itself can be extended through the use of Super Cache plugins. Ideally you should move the Super Cache plugins folder from its default location (wp-content/plugins/wp-super-cache/plugins/) as otherwise whenever you upgrade the super cache plugin it will overwrite its plugins directory.

I suggest moving the folder to wp-content/wpsc-plugins/. You’ll then need to edit the wp-content/wp-cache-config.php file to change the location where WP Super Cache looks for the plugins. Find the line that says:

$wp_cache_plugins_dir = WPCACHEHOME . 'plugins';

and change it to:

$wp_cache_plugins_dir = WP_CONTENT_DIR . '/wpsc-plugins';
Posted on by xoogu, last updated

Differences between caching methods with Geo targeted content

I recently released a version of the Geo Text WordPress plugin with support for the WP Super Cache and W3 Total Cache page caching plugins. As part of this I added a page to describe why caching plugins are problematic and the different methods the plugin uses to enable compatibility with caching plugins: Geo Text WordPress Plugin: W3TC and WPSC compatibility.

After writing that text, I thought some simple diagrams might help illustrate the reason why standard caching is not useful when you have geo targeted content, and the way that page key modification and fragment caching / dynamic caching can solve this issue.

Continue reading

Posted on by xoogu, last updated

How to set up a W3TC mirror CDN for WP multisite with domain mapping

WordPress Multisite with the domain mapping plugin is a good way to run multiple wordpress sites. It means you don’t have to worry about keeping WordPress and plugins up to date for each individual site.

The W3 Total Cache plugin is one of the most popular wordpress plugins for speeding up wordpess websites. One of the things it offers is serving static content from a CDN (Content Delivery Network). You don’t have to use a real CDN, but can instead a generic mirror CDN.

A generic mirror CDN doesn’t have all the benefits of a real CDN. However, because you use a different domain (or subdomain) for the CDN address, it does solve the problem of cookies being sent for static content requests. And unlike a real CDN, you don’t need to pay anything extra for it.

Continue reading

Posted on by xoogu, last updated