Skip to content

Instantly share code, notes, and snippets.

@PatelUtkarsh
Last active March 6, 2025 13:47
Show Gist options
  • Save PatelUtkarsh/ad5db0c9aa4bc20c50ef1bd33867e41a to your computer and use it in GitHub Desktop.
Save PatelUtkarsh/ad5db0c9aa4bc20c50ef1bd33867e41a to your computer and use it in GitHub Desktop.
Photon use custom CDN.
<?php
$cdn_image_domain_to_use = 'https://{replace-me}.cloudfront.net'
$cdn_image_domain_to_use_parsed = wp_parse_url( $cdn_image_domain_to_use );
// Stop creating crops and just use the original image.
add_filter( 'jetpack_photon_noresize_mode', '__return_true' );
// Setup custom domain.
add_filter( 'jetpack_photon_domain', fn() => $cdn_image_domain_to_use );
// Trick jetpack to use our domain. See https://github.com/Automattic/jetpack/blob/946220362c7db84cad03c7fae4c76c5930b46fd5/projects/packages/image-cdn/src/class-image-cdn-core.php#L163-L175
add_filter(
'jetpack_photon_pre_image_url',
function ( $image_url ) use ( $cdn_image_domain_to_use_parsed ) {
// Replace host with cloudfront.
$parse_url = wp_parse_url( $image_url );
return str_replace( $parse_url['host'], $cdn_image_domain_to_use_parsed['host'], $image_url );
},
20
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment