Note: We wrote this article with advanced users in mind; you’ll need some coding knowledge to accomplish these tasks. As always, enable Jetpack Backup so you can easily revert your site in case of a mistake. Hooks are a way for one piece of code to interact with or modify another piece of code. They make up the foundation for how Jetpack interacts with WordPress Core.
If you have questions about how to use these yourself, ask the Jetpack community in the forums. If you’d like to hire help, reach out to a service like Codeable.
Site admins use Jetpack’s Site Accelerator — a lightning-fast, turnkey CDN for unlimited files — to speed up their websites while lowering bandwidth consumption.
No two sites are alike, so there may be times in which it makes sense to disable certain aspects of the CDN or change how it processes images. Today we’ll explore how to use hooks to modify the functionality of Site Accelerator and customize it for the specific needs of your site.
Before using hooks:
- It’s important to prefix your functions to avoid conflicts with themes or plugins. Here, we use the prefix jetpackblog_.
- There’s no need to edit theme files directly — use a functionality plugin to add code snippets safely.
CDNs cache images, which can cause problems while you’re testing or experimenting with design changes. You can prevent Jetpack from processing any images while making edits with the following snippet:
add_filter( 'jetpack_photon_development_mode', '__return_true' );
How to disable Site Accelerator
When you pair the powerful jetpack_photo_skip_image hook with some of the WordPress conditional functions, you get very granular control over when and where to serve images from the CDN.
You may wish to disable the CDN if you use the same file name to represent images that periodically change — for example, a header image that alternates weekly, but whose filename is always headerimage1.jpg. Site Accelerator doesn’t clear its cache; instead, it caches images forever. To change an image, it must have a new file name, so the usual cache-clearing techniques won’t work.
Disabling the image CDN on pages
How to disable Jetpack CDN on specific pages:
To disable the CDN on specific pages, use the following conditional logic, alongside the jetpack_photo_skip_image hook.
function jetpackblog_101() {
if ( is_page( 101 ) ) {
add_filter( 'jetpack_photon_skip_image', '__return_true' );
}
}
add_action( 'wp', 'jetpackblog_101' );
Or, the is_page() function can also be passed a slug.
function jetpackblog_home() {
if ( is_page( 'home' ) ) {
add_filter( 'jetpack_photon_skip_image', '__return_true');
}
}
add_action( 'wp', 'jetpackblog_home' );
Disabling the image CDN on posts
The is_single() function is very similar to the is_page() function and works for any post type, except attachments and pages.
How to disable Jetpack CDN on specific posts
This is very similar to the page example above, but applies for single posts.
function jetpackblog_302() {
if ( is_single( 302 ) ) {
add_filter( 'jetpack_photon_skip_image', '__return_true' );
}
}
add_action( 'wp', 'jetpackblog_302' );
Control caching based on image URL
The jetpack_photon_skip_image filter is not the only way to control which images the CDN caches. By default, Jetpack caches both on-site and off-site images, so if you hotlink an image from an external site, you’ll also add it to the CDN cache.
How to force Jetpack CDN to cache images on your own server
If you serve a dynamic image from an external site (e.g. the image changes periodically but the URL stays the same), the CDN cache will not update. In this situation, you may want to only cache internal images.
function jetpackblog_external( $skip, $image_url, $args, $scheme ) {
// Get your site URL, without the protocol
$site_url = preg_replace( '~^(?:f|ht)tps?://~i', '', get_site_url() );
/**
* If the image URL is from your site,
* return default value (false, unless another function overwrites).
* Otherwise, do not use the CDN
*/
if ( strpos( $image_url, $site_url ) ) {
return $skip;
} else {
return true;
}
}
add_filter( 'jetpack_photon_skip_for_url', 'jetpackblog_external', 9, 4 );
How to change the way Jetpack CDN processes images
By default, Jetpack automatically compresses images uploaded to the CDN. But some sites — say, a photography blog — may want to display images at the original quality, regardless of file size. This is where the jetpack_photon_pre_args filter comes in.
function sa_custom_params( $args ) {
$args['quality'] = 100;
return $args;
}
add_filter( 'jetpack_photon_pre_args', 'sa_custom_params' );
In the above example, the quality parameter is set to 100, which means images will be uploaded at full quality. If you want to make even more customizations, there are a variety of supported query arguments to fine-tune how Jetpack processes your images.
Some brands may want to apply the same filters and setting to all images for consistency. Let’s take a look at one example, where the image quality is set to 100, a sepia filter is applied, and the metadata is stripped.
function sa_custom_paramsfilter( $args ) {
$args['quality'] = 100;
$args['filter'] = 'sepia';
$args['strip'] = 'all';
return $args;
}
add_filter( 'jetpack_photon_pre_args', 'sa_custom_paramsfilter' );
Customize Jetpack’s Site Accelerator with hooks
Jetpack’s Site Accelerator can improve the performance of your site. But sometimes, you need to customize its functionality to fit your specific situation. With a little bit of knowledge, you can create custom rules to optimize the way you use the CDN. Learn more about using conditional tags on your WordPress site or browse other hooks for CDN.
Thanks for this. I have been unable to enable the CDN due to real time images being impacted. Since they’re from off-site, I can now exclude them. Mo’ speed – Mo’ better!
LikeLike
Cool stuff:-)
LikeLike
I’m wondering how the Jetpack plans and features relate to WordPress.com
LikeLike
Are you interested in a specific WordPress.com plan? You can check the different features offered by each WordPress.com plan here:
https://wordpress.com/support/plan-features/
You can compare those with the features available in Jetpack plans here:
LikeLike
Jetpack plugins have really helped with the optimization of shoreloop. I’d be grateful if you help me with instruction on how to upgrade to the updated version of jetpack
LikeLike
You should be able to update to the most recent version of any plugin by going to Dashboard > Update in your site’s dashboard. If your site is hosted with a hosting provider that manages your installation of WordPress and your plugins for you, it may be that everything is already up-to-date, but don’t hesitate to check just to be sure.
LikeLike
I tried to change the way Jetpack CDN processes images using the code shown above. After uploading the functions file to my child theme I cleared the WP-Rocket cache (and my CDN). However, I see no change in image quality.
LikeLike
Did you use the code snippet to change the CDN image quality from the default
90
to100
? By default, Jetpack’s image CDN only applies lossless image optimizations, so you indeed won’t notice much of a difference visually. You would have to lower the quality below80
to start seeing a quality loss.LikeLike
Yes, I set it to 100. Problem is that on desktop, especially, the details and text of some photos looks rather blurry. For instance on this page: https://www.dutchamsterdam.nl/296-amsterdam-museum-bags-purses as well as on the home page.
Perhaps I should not upload compressed images? (I use RIOT, set at 80%)?
LikeLike
I wonder if this may happen because of the width settings in the Image block. If you’d like us to run some more tests with you to get to the bottom of this issue, do not hesitate to contact us via this contact form!
Thank you.
LikeLiked by 1 person