我创建了一个主题。我想通过api添加更新功能
如果使用主题更新, 则在使用主题时该通知应可见
实际上我不知道, 所以请给我你的建议或给我一个代码, 以便我可以添加更新主题的功能。
注意:如果你不了解api, 则可以提供其他代码
#1
你可以在此处使用site_transient_update_themes:
add_filter ( 'site_transient_update_themes', 'theme_check_for_update' );
function theme_check_for_update ( $transient ) {
// Check Theme is active or not.
if( empty( $transient->checked['Your-Theme-Name'] ) )
return $transient;
$request = theme_fetch_data_of_latest_version();
if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
return $transient;
} else {
$response = wp_remote_retrieve_body( $request );
}
$data = json_decode( $response );
if ( version_compare( $transient->checked['Your-Theme-Name'], $data->new_version, '<' ) ) {
$transient->response['Your-Theme-Name'] = (array) $data;
add_action('admin_notices', 'theme_update_admin_notice');
}
return $transient;
}
function theme_fetch_data_of_latest_version() {
// Your API call to check for new version
$request = wp_safe_remote_get( 'https://yourdomain.com/api/upgrade-json/' );
/*
Response Shoul be in following format:
{
"new_version": "1.0.4", "url": "https://yourdomain.com/theme/changelog/", "package": "https://yourdomain.com/theme/theme.zip"
}
*/
return $request;
}
function theme_update_admin_notice(){
echo '<div class="notice notice-warning notice-alt is-dismissible">
<p>New Theme Update is available.</p>
</div>';
}
评论前必须登录!
注册