WordPress Theme Developers Tip – Theme Update Noticifications

While doing some work for a client, they were wanting to tie into the update notifications that a user gets when a theme has an updated version in the WordPress theme directory. The catch is this was a premium theme that was being sold so couldn’t be submitted to the directory. While not as robust as the WordPress update system it doesn’t need to be most premium themes require you to log in to download updates. So a simple notification was sufficient. Please read more to see code and explanations.

<?php
add_action('admin_head','theme_check_ver'); //Theme Update Function
/****************************
Update Noticication Script
By Jeremy Clark

http://clark-technet.com

License: GPL
*****************************/

/*********************VALUES BELOW NEED TO BE CHANGED*************************/
$theme_sn= "theme-short-name";
//Theme's short name for adding options to database
$remote_file = "http://address-to-remote-server/theme-version.txt";
//Address of remote file with version number
$update_check_int = 24; 
// Time in hours to check file for new version
$theme_update_notice ="<p>New theme version available.  Please visit here for the download.</p>";
//Text for notice displayed to user about new version
/*********************END EDITING*********************************************/

//These variables don't need editing
$theme_data = get_theme_data(TEMPLATEPATH . '/style.css');
$local_version = $theme_data['Version'];
$update_last_check = get_option($theme_sn.'_last_ver_check');
$new_ver_notice = get_option($theme_sn.'_new_ver');


function theme_check_ver() {
	global $update_check_int, $update_last_check,$new_ver_notice,$theme_sn;
		if ($new_ver_notice = true) {
			add_action('admin_notices','theme_new_ver');
		}
		$update_check_int_seconds = $update_check_int * 3600;
		$now = time();
		if ( empty( $update_last_check ) ) {
				//first run
				theme_compare_ver();
				add_option($theme_sn.'_last_ver_check', $now);
			} else {
				$time_ago = $now - $update_last_check;
				if ( $time_ago > $update_check_int_seconds ) {
					theme_compare_ver();
					update_option($theme_sn.'_last_ver_check', $now);
				}
			}
}
function theme_compare_ver() {
	global $remote_file, $local_version,$theme_sn;
		$remote_text = file_get_contents($remote_file);
		if ($remote_text !== false) {
			$remote_version = $remote_text;
		if ($local_version == $remote_version) {
				delete_option($theme_sn.'_new_ver');
			} else {
				if ( is_numeric(str_replace(".", "", $local_version)) && is_numeric(str_replace(".", "", $remote_version)) ) {
					if ( substr_count($local_version, '.') == 0 ) {
						// x -> x.x.x.x
						$local_version = $local_version . '.0.0.0';
					} else if ( substr_count($local_version, '.') == 1 ) {
						// x.x -> x.x.x.x
						$local_version = $local_version . '.0.0';
					} else if ( substr_count($local_version, '.') == 2 ) {
						// x.x.x -> x.x.x.x
						$local_version = $local_version . '.0';
					}
					$local_version = str_replace(".", "", $local_version);
					//$local_version = substr($local_version,0,1) . '.' . substr($local_version,1,5);
					$local_version = '0.' . $local_version;
					//---------------//
					if ( substr_count($remote_version, '.') == 0 ) {
						// x -> x.x.x.x
						$remote_version = $remote_version . '.0.0.0';
					} else if ( substr_count($remote_version, '.') == 1 ) {
						// x.x -> x.x.x.x
						$remote_version = $remote_version . '.0.0';
					} else if ( substr_count($remote_version, '.') == 2 ) {
						// x.x.x -> x.x.x.x
						$remote_version = $remote_version . '.0';
					}
					$remote_version = str_replace(".", "", $remote_version);
					//$remote_version = substr($remote_version,0,1) . '.' . substr($remote_version,1,5);
					$remote_version = '0.' . $remote_version;
					//---------------//
					//echo $remote_version . ' - ' . $local_version . '<br />';
					if ( $remote_version > $local_version ) {
						add_action('admin_notices','theme_new_ver');
						add_option($theme_sn.'_new_ver',true);
					} else {
						del_option($theme_sn.'_new_ver');
					}
				} else {
					add_action('admin_notices','theme_new_ver');
					add_option($theme_sn.'_new_ver',true);
				}
			}
		} else {
			echo "Can't Connect to update server";
		}
}	

function theme_new_ver() { 
	global $theme_update_notice, $pagenow;
	if ( $pagenow == "themes.php") {
?>
		<div id="message" class="updated fade">
		<?php echo $theme_update_notice; ?>
		</div>
<?php
	}
}
?>

Then the update-notice.php file will need to be included from your functions.php file in your theme.

if ( is_admin() ) {
    include_once (TEMPLATEPATH . '/update-notice.php');
}

There are a few values in the top of the file that would need to change to reflect the theme it’s checking for. The theme-version.txt on the remote server simply contains the theme version that is available for release. The script checks and if it’s lower than the remote version it will display the $theme_update_notice variable with the text that you specify on the top of the themes.php page.

/*********************VALUES BELOW NEED TO BE CHANGED*************************/
$theme_sn= "theme-short-name";
//Theme's short name for adding options to database
$remote_file = "http://address-to-remote-server/theme-version.txt";
//Address of remote file with version number
$update_check_int = 24; 
// Time in hours to check file for new version
$theme_update_notice ="<p>New theme version available.  Please visit here for the download.</p>";
//Text for notice displayed to user about new version
/*********************END EDITING*********************************************/

About Jeremy Clark

Small town IT worker with interests in all things technological and technical. Biggest interests are in web development especially the WordPress publishing platform and the community supporting it. Currently developing and maintain the free WordPress theme Techozoic. I'm also always available for hire.
This entry was posted in tips, wordpress and tagged , , , , , . Bookmark the permalink.

4 Responses to "WordPress Theme Developers Tip – Theme Update Noticifications"

Leave a reply

Comments will be sent to the moderation queue.