apply_filters ( 'jetpack_photon_skip_image', bool false, string $src, string $tag )

Allow specific images to be skipped by Photon.

Source file: jetpack_vendor/automattic/jetpack-image-cdn/src/class-image-cdn.php

View in GitHub

Parameters

false

(bool) false Should Photon ignore this image. Default to false.

$src

(string) Image URL.

$tag

(string) Image Tag (Image HTML output).


Changelog

Since: Jetpack 2.0.3


How to use this hook

See Using actions and filters to customize Jetpack.

Notes

To exclude an entire post or page from using Photon, you can modify this snippet to fit your needs and add it to your theme’s functions.php or a core functionality plugin:

function no_photon_by_page() {
  if ( is_page( 2 ) ) {
    add_filter( 'jetpack_photon_skip_image', '__return_true');
  }
}

add_action('wp', 'no_photon_by_page');

In this example, Photon won’t be used on the page with the ID of 2, but you can use any of the WordPress Conditional Functions.

If you’d like to exclude an image from being cached and served by Photon, you can add the following code to your theme’s functions.php file:

function my_photon_exception( $val, $src, $tag ) {
        if ( $src == 'YOUR_IMAGE_URL' ) {
                return true;
        }
        return $val;
}
add_filter( 'jetpack_photon_skip_image', 'my_photon_exception', 10, 3 );

To exclude images from a specific domain, you can use the following code:

function htdat_photon_exception_for_domain ( $val, $src, $tag ) {
        $parse = parse_url( $src ); 
        $img_domain = $parse[ 'host' ]; 
        
        // specify the domain you want to exclude here. Note: www and non-www are different
        if ( $img_domain == 'domain.com' ) { 
                return true;
        }

        return $val;
}
add_filter( 'jetpack_photon_skip_image', 'htdat_photon_exception_for_domain', 10, 3 );

Have a note to contribute?

Comments

  1. heri wahyudianto says:

    in single.php, I need featured image only skip photon. not all image. how to modify ‘YOUR_IMAGE_URL’ ?

  2. Gwyneth Llewelyn says:

    Site accelerator (“Photon”), as well as WordPress itself, doesn’t like SVG. Thankfully, a few plugins on the library allow sanitised SVG to be uploaded and used as any other image file format. The trouble is that Photon gets quite confused with SVG… here is an attempt to get it to ignore any URL ending with .svg:

    
    function photon_skip_svg ( $val, $src, $tag ) {
        $img_file = parse_url($src, PHP_URL_PATH); // returns false if unparseable
        
        // if it ends with 'svg', let Photon ignore it
        if ( $img_file !== false && strripos( $img_file, ".svg", strlen( $img_file ) - 4 ) !== false ) { 
            return true;
        }
    
        return $val;
    }
    add_filter( 'jetpack_photon_skip_image', 'photon_skip_svg', 10, 3 );
    

    It sort of works for me with new SVGs; already existing ones might require some cache purging…