Skip to content

Instantly share code, notes, and snippets.

@ohryan
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohryan/906d7e715356d4e9c2d7 to your computer and use it in GitHub Desktop.
Save ohryan/906d7e715356d4e9c2d7 to your computer and use it in GitHub Desktop.
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