“Update Notice” for Self-hosted Themes

update-notice-sample

You’ll get a “update notice” for each theme hosted in wordpress.org theme directory if there is a new version.

For some reasons, your themes are not hosted in wp.org but what if you still want to add the notice to your users? I’ll show you how.

Before going to the dirty part, we’ll first need to know how WordPress execute the automatic update.

All installed themes version data is stored in a transient _site_transient_update_themes. Let’s take a look what’s out there with var_dump( get_option('_site_transient_update_themes') );.

theme-version-data

As you can see, the ‘checked‘ object contains an array of all version numbers for those themes installed on your site.

The data will change periodically or once “update_themes” action triggered, either upload a new theme, upgrade a theme, etc. Take ‘twentysixteen’ for example, now I am installing the latest version ‘1.2’. If I manually edit the version number in styles.css to ‘1.1’, and refresh the themes.php page in admin, the dump information is like this:

theme-version-data-1

Here, ‘checked’ version number for twentysixteen is 1.1 while the latest version is 1.2 in wordpress.org, so the response object was injected with information for the latest version incl theme (theme name), new_version, url (details url) and package (downloadable url).

I hope this helped you understand the logic: _site_transient_update_themes option stores all the theme metadata, checked object contains the installed theme versions; if there is a new version found, the latest version data will be added to the response object, and the update notice will appear to that specific theme with the information.

Given our themes are not hosted in wordpress.org, WordPress is not able to get the latest version data, so we need to manually add this data to the _site_transient_update_themes option.

We can make this possible with the pre_set_site_transient_update_themes filter (https://developer.wordpress.org/reference/hooks/pre_set_transient_transient/).

Okay, below is the working codes used in my pfun theme.

L9: $url is the JSON file we use to store the theme metadata.

{
    "pfun": {
        "new_version": "1.0",
        "url": "http://ptheme.com/item/pfun/"
    },
    "pmag": {
        "new_version": "1.1",
        "url": "http://ptheme.com/item/pmag/"
    },
    "pviral": {
        "new_version": "1.1",
        "url": "http://ptheme.com/item/pviral/"
    },
    "amypink": {
        "new_version": "1.0",
        "url": "http://ptheme.com/item/amypink/"
    }
    
}

L10-14: Get the JSON info from the URL and convert it to an array for easy use.
L16-19: Compare the installed version against the version from the url, if it’s less than the latest version, add the data to $transient response object.
L20: Return the update transient value.

Let’s test it. Assume I am installing an older version lets say 0.9 of pfun. Get back to the dashboard and select the pfun theme. Bingo, the update notice is there as expected!
theme-update-02

theme-update-01

Also the pre_set_site_transient_update_themes option value changes.

theme-version-data

As I did’t add a ‘package’ value, so it says ‘Automatic update is unavailable for this theme.‘. The automatica update should be available if the ‘package’ url is set. Give it a try!

Save

Author: Leo

My name is Leo who's behind this blog. I've been working with wordpress since 2010. I'll write about themes, plugins, tutorials and all about wordpress here. Hope you find it useful!

1 thought on ““Update Notice” for Self-hosted Themes”

Leave a Reply

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