Adding Update Icons for Commercial Plugins

If you sell premium plugins, or even just host your plugins anywhere other than on the WordPress.org plugin repository, you may have run into something like the following when you update your plugins…

WordPress Plugin Update Icons

Anyone who’s updated their plugins via the wp-admin has seen those little icons next to the available updates. But why do some have actual images, while others have the generic plug icon? This has to do with what the WordPress.org plugins API returns, and how WordPress core processes/displays that data. Adding these icons is pretty straightforward if your plugin is hosted on .org – just add the files to the assets directory of the plugin’s SVN repo.

Unfortunately, it’s not so simple if your plugin is hosted elsewhere (e.g. premium plugins). For example, if you’re using Easy Digital Downloads, along with Software Licensing, to sell licenses for a premium plugin, you’re able to tap into the WordPress updates API. However, the EDD Software Licensing API response does not, by default, include data about these icons. Further, the Software Licensing integration class that you include in your plugin does not support processing those icons.

This means that, to add those little branded icons to plugin updates via EDD Software Licensing, we have to add the correct data to the API response, and process the data in our plugin so that customers will see our chosen icons when there’s an update available. Let’s get into it!

First up, upload this bit of code to your own website. I have not included any code for actually retrieving icon URLs, so you’ll need to handle this yourself (e.g. Download meta data).


			<?php

/**
 * Filter the EDD license response data to include icons for our Downloads.
 * This has to be run from your server.
 * For testing, you may have to clear edd_api_request_{%} transient key (3 hours) on the client side.
 *
 * @param (array) $response - All response data (e.g. new_version, package, etc.)
 * @param (object) $download - EDD_SL_Download
 *
 * @return (array) $response
 *
 * @author Ren Ventura (renventura.com)
 */
function rv_eddsl_api_response_add_update_icons( $response, $download ) {

	// Retrieve icon URLs

	$response['icons'] = serialize( array(
		'svg' => '',
		'1x' => '',
		'2x' => '',
		'default' => ''
	) );

	return $response;
}
add_filter( 'edd_sl_license_response', 'rv_eddsl_api_response_add_update_icons', 5, 2 );		

Next, add this snippet to your plugin. This is run by your customers’ websites.


			<?php

/**
 * Filter the transient data for our plugin's icons.
 * Since icons are passed back as serialized arrays, we need to unserialize them.
 * This has to be run from within your plugin.
 *
 * @param (object) $transient - Full transient data
 * @return (object) $transient
 *
 * @author Ren Ventura (renventura.com)
 */
function rv_update_plugins_transient_unserialize_icons( $transient ) {

	if ( is_object( $transient ) && isset( $transient->response ) && is_array( $transient->response ) ) {
		
		$basename = plugin_basename( __FILE__ );

		// Received a response for our plugin
		$plugin = isset( $transient->response[$basename] ) ? $transient->response[$basename] : new stdClass;

		// Are there any icons set for the plugin?
		if ( isset( $plugin->icons ) ) {
			$icons = is_string( $plugin->icons ) ? unserialize( $plugin->icons ) : $plugin->icons;
			$transient->response[$basename]->icons = $icons;
		}
	}

	return $transient;
}
add_filter( 'pre_set_site_transient_update_plugins', 'rv_update_plugins_transient_unserialize_icons', 99 );		

If you’re able to implement this, let me know how it works for you!

Leave a Reply

Your email address will not be published. Required fields are marked *