CDN compatibility with WPLMS

For Sites using CDN.

WPLMS uses standard WordPress functions to display images. However, when images are loaded via an ajax call it is possible that the images load directly from the server rather than the CDN.

 

The example below assumes PHOTON CDN, you’ll need to contact your corresponding CDN’s for help on this.

function static_counter( $url ) {
        srand( crc32( basename( $url ) ) );
        $static_counter = rand( 0, 2 );
        srand();
        return $static_counter;
}

add_filter ( 'post_thumbnail_html','wplms_cdn_ajax_image_handle',10,4);
function wplms_cdn_ajax_image_handle($html,$post_id,$post_thumbnail_id, $size){

	$url = wp_get_attachment_image_src($post_thumbnail_id,$size);
	if (strpos($url,'http://') !== false)
	{
	   $url=str_replace('http://','',$url);
	}

	return '<img src="http://i'.static_counter($url).'.wp.com/'.$url.'">';
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *