Disable Jetpack’s Photon module for Keying Social Importer

Jetpack Photon Toggle

Since migrating my site to a new server, I’ve been noticing mixed content SSL errors.

After doing a bit of digging, I found that when my Instagram posts were being imported by Keyring Social Importers, that the image source was being set with an http Jetpack Photon URL.

So, my post markup looked like this:

<img src="http://i2.wp.com/manofhustle.com/wp-content/uploads/2016/04/13130026_1061188440605739_999811134_n.jpg?fit=640%2C640&quality=80&strip=all" width="640" height="640" alt="Bees in a tree" class="keyring-img" />;

Instead of this:

<img src="https://eric.blog/wp-content/uploads/2016/05/13113885_1541803272790749_1295774627_n.jpg" width="640" height="640" alt="&quot;No more pictures. No! No! No! No!&quot;" class="keyring-img" />;

I wasn’t sure why the http was being used instead of https. That being said, I knew that the post content should have manofhustle.com for the image source instead of a Jetpack Photon URL. So, I set off to fix it.

Finding the fix

Within a few minutes, I stumbled across one of my coworker’s (Erick Hitter) blog posts about conditionally disabling Photon, which made the work pretty easy! Thanks Erick

In that article, Erick suggests this code to disable Photon conditionally:

$photon_removed = remove_filter( 'image_downsize', array( Jetpack_Photon::instance(), 'filter_image_downsize' ) );

// Call wp_get_attachment_image(), wp_get_attachment_image_src(), or anything else that ultimately calls image_downsize()

if ( $photon_removed )
add_filter( 'image_downsize', array( Jetpack_Photon::instance(), 'filter_image_downsize' ), 10, 3 );

Modifying for Keyring Social Importers

The above code works great when you know that Jetpack is installed and when you have access to whatever function is calling for the image source. But, in the case of a plugin like Keyring Social Importers, we need to find actions to use so that we don’t modify the plugin code.

So, after looking around a bit, I found the import() method in Keyring Social Importers which seems to kick the process off. Within that method, there are two actions which are useful to us – import_start and import_end.

With that in mind, I modified Erick’s code to specifically work with Keyring Social Importers.

add_action( 'import_start', 'moh_disable_photon_social_importer' );
function moh_disable_photon_social_importer() {
if ( ! class_exists( 'Jetpack_Photon' ) ) {
return;
}

remove_filter( 'image_downsize', array( Jetpack_Photon::instance(), 'filter_image_downsize' ) );
add_action( 'import_end', 'moh_enable_photon_social_importer' );
}

function moh_enable_photon_social_importer() {
add_filter( 'image_downsize', array( Jetpack_Photon::instance(), 'filter_image_downsize' ), 10, 3 );
}

You can place this code in a custom functionality plugin for your site or in the functions.php file of your theme.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.