Disable Jetpack’s Photon module in specific situations

I receive a lot of interesting questions about the Photon module in Jetpack. Occasionally, users would like to disable Photon for certain requests, such as when a plugin or theme calls one of WordPress’s image functions (wp_get_attachment_image(), wp_get_attachment_image_src(), or anything else that ultimately calls image_downsize()).

While the Photon module does include filters to disable it for each individual image requested (see here and here), there are situations where one may wish to disable Photon regardless of the image being requested, such as when a certain widget retrieves an image from WordPress’s media library.

Doing so is relatively simple, but a bit obfuscated because the Photon module is written as a PHP class. The snippet below demonstrates how to do so, and is made possible because the Photon module is implemented as a singleton.

$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 );
}

The code on line 1 should appear immediately before the function call that shouldn’t have Photon applied, and lines 5 and 6 should appear immediately afterwards to ensure that Photon is available for any subsequent calls to image functions.

Grab the snippet from https://git.ethitter.com/snippets/1.

Aside – if you’re curious about using singletons in WordPress, check out Eric Mann’s post on the subject: The Case for Singletons in WordPress.

Using shortlinks with Jetpack Sharing

At present, Jetpack doesn’t use shortlinks when visitors share your content through the built-in Sharing module. With length-limited services like Twitter, this can be a problem. Take, for example, http://ethitter.com/2013/05/using-shortlinks-with-jetpack-sharing/ versus http://eth.pw/s; the latter frees up many more precious characters.

Thankfully, the folks who built the Sharing module provided a filter to override the link used with the sharing buttons.

Add this snippet of code to your theme’s functions.php file, or a file in your site’s wp-content/mu-plugins directory, and your visitors will be sharing with shortlinks in no time.

/**
 * Force Jetpack's Sharing module to use a shortlink rather than full permalink
 *
 * From http://eth.pw/s
 *
 * @param string $url
 * @param int $post_id
 * @uses wp_get_shortlink
 * @filter sharing_permalink
 * @return string
 */
function eth_sharedaddy_shortlink( $url, $post_id ) {
	return wp_get_shortlink( $post_id );
}
add_filter( 'sharing_permalink', 'eth_sharedaddy_shortlink', 10, 2 );

Note that the Publicize module, which handles automatically sharing your content to your own connected social media accounts, does use shortlinks already; therefore, the code snippet above won’t have any impact on Publicize.

Also, I opted not to make this a plugin in anticipation of Jetpack making the switch to shortlinks at some point.

Authy for WordPress? Jetpack Photon for NextGEN Gallery?

I’ve been busy this week writing some new plugins.

First, I released Jetpack Photon for NextGEN Gallery, which extends the WordPress.com CDN capabilities in Jetpack to galleries built with the NextGEN Gallery plugin. I built this one after building Jetpack’s Photon module and noticing that NextGEN Gallery images weren’t benefitting from the Photon CDN.

Then, I enabled two-factor authentication on my CloudFlare account. CloudFlare uses a hosted service, Authy, rather than a local codebase, to handle the verification process using an installed app and a hosted API. Surprisingly, there wasn’t a WordPress plugin that integrated the service, but there is now: Authy for WordPress. It’s in use on this site, in fact.

Both plugins are available in the WordPress.org plugin repository. Development can be found at http://github.com/ethitter/Authy-for-WP and http://github.com/ethitter/jetpack-photon-for-nextgen, respectively.