Skip to content

Instantly share code, notes, and snippets.

@dancameron
Created September 19, 2012 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dancameron/3747376 to your computer and use it in GitHub Desktop.
Save dancameron/3747376 to your computer and use it in GitHub Desktop.
Theme Updater Example.
<?php
if ( is_admin() ) {
add_filter( 'pre_set_site_transient_update_themes', 'gb_check_for_premium_theme_update' );
}
function gb_check_for_premium_theme_update( $transient ) {
$current_version = 1.2;
$api_key = get_option('theme_api_key'); // your theme options will need to have an option to add the API key.
$theme_slug = 'theme_slug'; // Change to match with API Key Manager
$api_url = 'http://yoursite.com/check-key/'; // change to your site
$basename = basename( get_template_directory() );
// Get API response
$theme_data_api = wp_remote_post( $api_url, array(
'body' => array(
'key' => $api_key,
'plugin' => $theme_slug,
'url' => site_url(),
'wp_version' => get_bloginfo( 'version' ),
'plugin_version' => $current_version
),
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url('/')
) );
$theme_data = json_decode( wp_remote_retrieve_body( $theme_data_api ) );
if ( !is_wp_error( $theme_data_api ) && 200 == $theme_data_api['response']['code'] ) {
if ( version_compare( $current_version, $theme_data->version, '<' ) ) {
if ( version_compare( $current_version, $theme_data->version, '<' ) ) {
$transient->response[$basename]['url'] = $theme_data->url;
$transient->response[$basename]['slug'] = $basename;
$transient->response[$basename]['package'] = $theme_data->download_url;
$transient->response[$basename]['new_version'] = $theme_data->version;
}
}
}
return $transient;
}
// set_site_transient('update_themes', null); // uncomment for the updater to load everytime, used for testing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment