Wordpress 为我的主题制作更新通知功能
我已经创建了一个主题。我想通过api添加更新功能 当我使用主题时,如果主题更新需要,通知应该是可见的 实际上我不知道,所以请给我你的建议或给我一个代码,这样我就可以添加更新主题的功能Wordpress 为我的主题制作更新通知功能,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我已经创建了一个主题。我想通过api添加更新功能 当我使用主题时,如果主题更新需要,通知应该是可见的 实际上我不知道,所以请给我你的建议或给我一个代码,这样我就可以添加更新主题的功能 注意:如果您不了解api,因此可以提供另一个代码您可以在此处使用站点更新主题: add_filter ( 'site_transient_update_themes', 'theme_check_for_update' ); function theme_check_for_update ( $transient
注意:如果您不了解api,因此可以提供另一个代码您可以在此处使用
站点更新主题
:
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>';
}
add_filter('site_transient_update_themes'、'theme_check_for_update');
函数主题检查更新($transient){
//检查主题是否处于活动状态。
if(空($transient->checked['Your-Theme-Name']))
返回$transient;
$request=theme\u fetch\u data\u的最新版本();
如果(是wp错误($request)| wp远程检索(request)(代码($request)!=200){
返回$transient;
}否则{
$response=wp\u remote\u retrieve\u body($request);
}
$data=json_decode($response);
如果(版本比较($transient->checked['Your-Theme-Name'],$data->new_version,'我不想这样做。我想和默认主题一样做。你知道吗?它和默认主题一样。如果你转到外观>主题,你可以在那里看到主题更新通知。是的,当然你是对的。但是我想在我的主题替换你的主题名称中添加相同的功能代码>与你的主题名称。好吧,好吧,我的代码有点变化,它的工作很好