Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress plugin to fix responsive images, credit Project Nami. Adapted to conform to WordPress coding standards.
<?php
/*
Plugin Name: Azure WordPress Storage Responsive Images Fix
Plugin URI: http://projectnami.org/fix-for-azure-storage-plugin-and-wp-4-4/
Description: Corrects srcset URLs for responsive images in a Scalable WordPress install running on Microsoft Azure.
Author: Project Nami / Doug Vanderweide
Version: 0.1
Author URI: https://www.dougv.com/2016/01/fixing-wordpress-4-4-responsive-images-in-azure-scalable-wordpress-installs/
License: GNU GPL 2
*/
function windows_azure_storage_wp_calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id )
{
$media_info = get_post_meta( $attachment_id, 'windows_azure_storage_info', true );
if ( ! empty( $media_info ) ) {
foreach ( $sources as &$source ) {
$img_file_name = substr( $source['url'], strrpos( $source['url'], '/' ) + 1 );
if ( $img_file_name == substr( $media_info['blob'], strrpos( $media_info['blob'], '/' ) + 1 ) ) {
$source['url'] = "https://" . get_option( 'azure_storage_account_name' ) . ".blob.core.windows.net/" . $media_info['container'] . "/" . $media_info['blob'];
} else {
foreach ( $media_info['thumbnails'] as $thumbnail ) {
if ( $img_file_name == substr( $thumbnail, strrpos( $thumbnail, '/' ) + 1 ) ) {
$source['url'] = "https://" . get_option( 'azure_storage_account_name' ) . ".blob.core.windows.net/" . $media_info['container'] . "/" . $thumbnail;
break;
}
}
}
}
}
return $sources;
}
add_filter( 'wp_calculate_image_srcset', 'windows_azure_storage_wp_calculate_image_srcset', 10, 5 );
?>
@dougvdotcom

This comment has been minimized.

Copy link
Owner Author

dougvdotcom commented Jan 5, 2016

Credit to original author and issue reporters, http://projectnami.org/fix-for-azure-storage-plugin-and-wp-4-4/

@jamesdixon

This comment has been minimized.

Copy link

jamesdixon commented Apr 7, 2016

@dougvdotcom I added this plugin and got the error message mentioned in your blog post. I have the latest version of the Azure Storage plugin (3.0.1) and Wordpress 4.4.2, but the srcset property is still being rewritten incorrectly (adding /wp-content/uploads to the url). Any suggestions?

Cheers.

@dougvdotcom

This comment has been minimized.

Copy link
Owner Author

dougvdotcom commented Apr 12, 2016

James, you don't need to run this code after Feb. 9. Microsoft fixed the problem on that date with an update of the mandatory storage plugin. Make sure your install is running Windows Azure Storage for WordPress version 3.0.1. If it is, see the blog post for troubleshooting advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.