Last active
August 29, 2015 14:27
-
-
Save ohryan/906d7e715356d4e9c2d7 to your computer and use it in GitHub Desktop.
Use Jetpack Photon with arbitrary URLs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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