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]
<?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
}
}
?>
[/php]
Then the update-notice.php file will need to be included from your functions.php file in your theme.
[php light=”true”]
if ( is_admin() ) {
include_once (TEMPLATEPATH . ‘/update-notice.php’);
}
[/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.
[php]
/*********************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*********************************************/
[/php]

By Jesse January 18, 2011 - 12:54 pm
Thanks for the code Jeremy. Always looking for new ways to add value to for my customers. One of the things I love about being a designer is that everyone is so generous with their time and expertise. Good Karma, for sure!
By Frankie Jarrett April 7, 2011 - 10:18 am
This is very useful! Thank you for sharing.
Have a few problems though…
1. It’s still showing the message even if the versions match?
2. If I customize the $theme_sn var I get an error…no error if I use the default ‘theme-short-name’ value
Fatal error: Call to undefined function del_option() on line 89
//Frankie
By Jeremy Clark April 17, 2011 - 5:47 pm
I’ve put together a better solution with this package. It works for plugins and themes. And allows for using the builtin theme and plugin updater.
By alban October 30, 2011 - 8:41 am
Tnx for help.Very nice explanation.