Skip to content

Instantly share code, notes, and snippets.

@slopesweb
Forked from kloh-fr/update.json
Created September 7, 2022 03:07
Show Gist options
  • Save slopesweb/20248fa2dc6ba110f8b7dcca78e125e8 to your computer and use it in GitHub Desktop.
Save slopesweb/20248fa2dc6ba110f8b7dcca78e125e8 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