Skip to content

Instantly share code, notes, and snippets.

@niemenmaa
Created August 30, 2017 07:11
Show Gist options
  • Save niemenmaa/17fd3fdb6f9e4d3d51d0d22b90053532 to your computer and use it in GitHub Desktop.
Save niemenmaa/17fd3fdb6f9e4d3d51d0d22b90053532 to your computer and use it in GitHub Desktop.
Enable srcset when wp-stateless and Google Bucket is used
add_filter( 'max_srcset_image_width', __NAMESPACE__ . '\\remove_max_srcset_image_width', 99 );
/**
* Reset max srcset image width.
*
* @param int $max_width The maximum image width to be included in the 'srcset'. Default '1600'.
*
* @return bool You can return sensible value also this resets to default 1600.
*/
function remove_max_srcset_image_width( $max_width ) {
return false;
}
add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\\stateless_cdn_srcset' );
/**
* Wp-stateless can't generate it's own google bucket links so we need to create them manyally.
*
* @param array $sources One or more arrays of source data to include in the 'srcset'.
*
* @return array
*/
function stateless_cdn_srcset( $sources ) {
if ( is_plugin_active( 'wp-stateless/wp-stateless-media.php' ) ) {
foreach ( $sources as $source ) {
// Default upload path
$wp_upload = wp_upload_dir();
$bucket_name = 'change this';
$bucket_baseurl = 'https://storage.googleapis.com/' . $bucket_name;
// Find Wordpress' default upload path and replace it with Google bucket path.
$sources[ $source[ 'value' ] ][ 'url' ] = str_replace( $wp_upload[ 'baseurl' ], $bucket_baseurl, $sources[ $source[ 'value' ] ][ 'url' ] );
}
}
return $sources;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment