Add an “Update notification” for self-hosted premium themes or plugins

Recently we embarked on the project of creating our own premium theme (Xclusive -Pro) that extends the basic functionality of our image store plugin.

One thing that we love about WordPress is how users can easily update themes, and we wanted to offer the same functionally to anyone using our premium theme.

There was not a lot of information in how to do it. Reason why we are writing this post, we hope this save people a little bit of valuable time.

 

Server set up

The first thing that is needed is a place where to store and manage the version history. This was easy for us since we already save the information on every release in our site.

Next, you will need to set a link that returns the latest release information in a jSON format.

{
    "new_version":"1.0.1",
    "url":"http:\/\/domain.com\/link-to-theme-or-plugin\/",
    "package":"http:\/\/domain.com\/link-to-plugin-0.1.0.zip"
}

 The hook

After you completed the server setup is time to add a hook to the premium theme or plugin. We are going to hook into pre_set_transient_{$transient}. If you look at the documentation you may ask yourself, what is “$transient”? WordPress uses this hook for many things, we are just concern about the update options bellow:

  • pre_set_site_transient_update_themes
  • pre_set_site_transient_update_plugins
  • pre_set_site_transient_update_core

Use the hook for the type of project that you are working on, as you can see you can also provide a different link to a custom WordPress core installation. For this tutorial will use “update_themes“.

 

The Code

  • Start with a basic hook and function.
function theme_pre_set_transient_update_theme ( $transient ) {

} 
add_filter ( 'pre_set_site_transient_update_themes', 'theme_pre_set_transient_update_theme' );
  • Double check that the theme is active and valid.
function theme_pre_set_transient_update_theme ( $transient ) {
 if( empty( $transient->checked['theme-name'] ) )
    return $transient;
} 
add_filter ( 'pre_set_site_transient_update_themes', 'theme_pre_set_transient_update_theme' );
  • Get the jSON information from the server.
function theme_pre_set_transient_update_theme ( $transient ) {
 if( empty( $transient->checked['theme-name'] ) )
    return $transient;

  $ch = curl_init();
 
  curl_setopt($ch, CURLOPT_URL, 'http://domain.com/link-to-json-data' );
 
 // 3 second timeout to avoid issue on the server
 curl_setopt($ch, CURLOPT_TIMEOUT, 3 ); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 $result = curl_exec($ch);
 curl_close($ch);

 // make sure that we received the data in the response is not empty
 if( empty( $result ) )
   return $transient;

} 
add_filter ( 'pre_set_site_transient_update_themes', 'theme_pre_set_transient_update_theme' );
  • Finally, check the jSON data against the current version, if it is not the same allow WordPress to show an update notification for the self-hosted theme.
function theme_pre_set_transient_update_theme ( $transient ) {
 if( empty( $transient->checked['theme-name'] ) )
    return $transient;

  $ch = curl_init();
 
  curl_setopt($ch, CURLOPT_URL, 'http://domain.com/link-to-json-data' );
 
 // 3 second timeout to avoid issue on the server
 curl_setopt($ch, CURLOPT_TIMEOUT, 3 ); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 $result = curl_exec($ch);
 curl_close($ch);

 // make sure that we received the data in the response is not empty
 if( empty( $result ) )
   return $transient;

 //check server version against current installed version
 if( $data = json_decode( $result ) ){
   if( version_compare( $transient->checked['theme-name'], $data->new_version, '<' ) )
 $transient->response['theme-name'] = (array) $data;
 }
 
 return $transient;

} 
add_filter ( 'pre_set_site_transient_update_themes', 'theme_pre_set_transient_update_theme' );

That is it, you have successfully added a update notification to your premium theme.


Leave a Reply

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