How to Disable Jetpack Photon Caching in Specific Pages?

Categories WordPress TutorialsTags , , 7 Comments

Jetpack Photon is an image accelerator you can use to speed up the load times of your images comes from Jetpack. Photon is a great, easier and free option to speed up your Website load times by using super fast servers. Photon uses WordPress.com CDN servers to speed up your images load times.

To activate Photon, you need to install Jetpack plugin, authored by Automattic – the parent company of WordPress. After activating Jetpack, go to Jetpack > Settings > Performance and scroll down to Performance & speed options and then toggle on the option Enable site accelerator > Speed up image load times.

Jetpack Photon Module – What’re the Benefits of Photon CDN?

After activating Photon, images uploaded to your posts and pages are duplicated to a super fast content delivery network hosted on the WordPress.com servers. Photon works on every page on the activated Website.

Disable Jetpack Photon Caching in Specifics

Photon caches every image you upload in posts and pages content. To prevent Photon from caching in specific pages, you need to add these custom functions depending on your needs.

# Disable Jetpack Photon caching in singular pages – To disable Photon in singular pages (singular page includes single-post, single-page, single-item/single-post-type), you need to add this code snippet.

// Disable Photon caching in singular pages
function bydik_no_photon_by_singular() {
  if ( is_singular() ) {
    add_filter( 'jetpack_photon_skip_image', '__return_true');
  }
}
add_action('wp', 'bydik_no_photon_by_singular');

# Disable Jetpack Photon caching in pages – Add this code snippet to disable Photon in pages. Pages (not post, media etc.) will be ignored by Photon by using the code.

// Disable Photon caching in all pages
function bydik_no_photon_by_page() {
  if ( is_page() ) {
    add_filter( 'jetpack_photon_skip_image', '__return_true');
  }
}
add_action('wp', 'bydik_no_photon_by_page');

# Disable Jetpack Photon caching in specific pages – Add this code snippet to disable Photon in specific pages. Here 12, 18, 22 is page ID – you can change the value to your desired page ID.

// Disable Photon caching in specific pages
function bydik_no_photon_by_specific_page() {
  if ( is_page( array( 12, 18, 22 ) ) ) {
    add_filter( 'jetpack_photon_skip_image', '__return_true');
  }
}
add_action('wp', 'bydik_no_photon_by_specific_page');

# Disable Jetpack Photon caching in single post/media/portfolio pages – Add this code snippet to disable Photon in single post/media/portfolio pages.

// Disable Photon caching in single post/media/portfolio pages
function bydik_no_photon_by_single_post_page() {
  if ( is_single() ) {
    add_filter( 'jetpack_photon_skip_image', '__return_true');
  }
}
add_action('wp', 'bydik_no_photon_by_single_post_page');

# Disable Jetpack Photon caching in archive pages – Add this code snippet to disable Photon in archive pages.

// Disable Photon caching in archive pages
function bydik_no_photon_by_archive_page() {
  if ( is_archive() ) {
    add_filter( 'jetpack_photon_skip_image', '__return_true');
  }
}
add_action('wp', 'bydik_no_photon_by_archive_page');

# Skip Jetpack Photon for an image – Add this code snippet to disable Photon for an image. Photon will be disabled for the image URL. Don’t forget to change YOUR_IMAGE_URL with your image URL.

// Skip Jetpack Photon for an image file
function bydik_no_photon_for_an_image( $val, $src, $tag ) {
  if ( $src == 'YOUR_IMAGE_URL' ) {
    return true;
  }
  return $val;
}
add_action('wp', 'bydik_no_photon_for_an_image');

You can add these functions in your theme’s functions.php, your own plugin or Functionality plugin. If you need further help in filtering Photon caching, please comment us in the comment section.

7 Comments on this.

    • You can try this in this case, by using operator,

      function no_photon_for_an_image( $val, $src, $tag ) {
        if ( ($src == 'YOUR_IMAGE_URL') || ($src == 'YOUR_IMAGE_URL_2') ) {
          return true;
        }
        return $val;
      }
      add_action('wp', 'no_photon_for_an_image');

      Please reply if it doesn’t work, thanks.

  1. Is there any way to use the “# Skip Jetpack Photon for an image” to skip any image with a URL coming from a given domain?

    For example, Amazon doesn’t like anyone caching their images, as they do not allow external servers to store their images for more than 24 hours (intellectual property issues). The Jetpack Photon CDN caching those images defeats the purpose of hotlinking images to their sites. Being able to exclude images with a URL corresponding to a domain would fix the issue in that case. This is likely going to be a much bigger issue over the next few years even if Amazon isn’t cracking down on it too hard right now.

  2. Sorry, this does not work for me : (. Tried for page, single image…the source is still the i0 one.

    function no_photon_by_page() {
      if ( is_page( 113 ) ) {
        add_filter( 'jetpack_photon_skip_image', '__return_true');
      }
    }
    add_action('wp', 'no_photon_by_page');

    That should skip for the entire home page, but I still see i0. I placed this in a file required in functions.php (with other action / filter functions that I know are working).

  3. Thank you so much for these codes. I have added first code to skip photon on single post and pages so that google i can get traffic from google images.

Leave a Reply

Your email address will not be published.