Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use Jetpack Photon with arbitrary URLs
<?php
/*
This function allows you to use the Jetpack plugin's Photon CDN for arbitrary images in your theme.
Add this function to your functions.php, then call it by passing an image url.
ex. <img src="<?= photonify(get_template_directory_uri() . /images/logo.png) ?>" alt="company logo">
*/
function photonify( $src ) {
if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'photon' ) ) {
$url = parse_url( $src );
// it's relative, add the host
if ( !isset($url['host']) ) {
$hostname = $_SERVER['HTTP_HOST'];
$url = 'http://'.$hostname.$src;
} else {
$url = $src;
}
return apply_filters( 'jetpack_photon_url', $url );
}
return $src;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment