How to serve attachments from a free CDN (Coral)

You can use Coral free CDN to off-load the WordPress attachments (usually images). In order to use Coral you need to add “.nyud.net” to the site DNS address. For example, http://www.alekz.net will become http://www.alekz.net.nyud.net. Coral is usually too slow to serve all static content, so the most effective solution would be to use it only for the biggest files.

Here’s a simple solution. Add the following to functions.php of your current theme:

add_filter ('wp_get_attachment_url', 'freecdn_url');

function freecdn_url ($url)
{
    if (! is_attachment () ) return $url; 
    $cdn_url = ".nyud.net";
    $decomposed_url = explode ("/", $url);
    $decomposed_url[2] =  $decomposed_url[2] . $cdn_url;
    $url = implode("/", $decomposed_url);
    return $url;
}

Your theme (e.g. the bundled “TwentyTen”) must use wp_get_attachment_url(), of course. If not – you got the idea, didn’t you? πŸ˜‰

Also, you can use freecdn_url () function to rewrite any URL you want to download from Coral.