Skip to content

Instantly share code, notes, and snippets.

@kloh-fr
Last active September 7, 2022 03:07
Show Gist options
  • Save kloh-fr/474875b6208fcbddf158c7e39eeb9222 to your computer and use it in GitHub Desktop.
Save kloh-fr/474875b6208fcbddf158c7e39eeb9222 to your computer and use it in GitHub Desktop.
Update notifier function for a custom WordPress Theme
{
"new_version":"1.0.0",
"url":"http://www.domain.com/theme-name/changelog",
"package":"http://www.domain.com/theme-name/themes.zip"
}
<?php
/**
* Theme update notifier
*
* @author Luc Poupard
*
* Based on Xpark Media code :
* @link https://xparkmedia.com/blog/add-update-notification-selfhosted-premium-themes-plugins/
* But using wp_remote_get() instead of curl_init() :
* @link https://pippinsplugins.com/using-wp_remote_get-to-parse-json-from-remote-apis/
*/
function theme_notifier ( $transient ) {
if( empty( $transient->checked['theme-name'] ) )
return $transient;
$response = wp_remote_get( 'http://www.domain.com/theme-name/update.json' );
if( is_wp_error( $response ) ) {
return false;
}
$result = wp_remote_retrieve_body( $response );
$data = json_decode( $result );
if( ! empty( $data ) ) {
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_update_notifier' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment