我相信有很多小伙伴开始写自己的原创WordPress主题,在这个时候一般很多都是借助于插件或者直接手动推送给用户,不管您是不会写还是懒得折腾更新,或者怕在线更新功能会被盗版也提供更新了今天要说的方法很不错哟。
//请自行折腾


//主题更新功能
add_filter('pre_set_site_transient_update_themes', 'theme_update');
add_action('delete_site_transient_update_themes', 'deleteStoredData')  
function get_current_theme( $name=false ){
    $theme = wp_get_theme();
    if($theme->get('Template')){
        return $name ? $theme->parent()->get('Name') : $theme->template;
    }else{
        return $name ? $theme->get('Name') : $theme->stylesheet;
    }
}
function theme_update($value){
	$new_version='2.9.6';//新版
	$new_url='https://www.ruikeedu.com/chameleons/';//介绍页
	$new_package='https://www.ruikeedu.com/chameleons/xxx.zip';//下载包
	//以上数据可以定义为post接受参数来赋予,这样可以很好的保护我们主题下载包,你可以加授权验证账号网站等
	//注意下方修改变量
	$version='2.9.5';//旧版
    $state = get_option('update_chameleonS');
    if ( empty($state) ){
        $state = new StdClass;
        $state->lastCheck = time();
        $state->checkedVersion = $version;
        $state->update = null;
    }
    if(version_compare($version, $new_version)<0) {
        $state->update = new StdClass;
        $state->update->version = $new_version;
        $state->update->url = urldecode($new_url);
        $state->update->package = urldecode($new_package);
        update_option('update_chameleonS', $state);
    }
    global $theme_update_state;
    if(!isset($theme_update_state)) $theme_update_state = get_option('update_chameleonS');

    if ( !empty($theme_update_state) && isset($theme_update_state->update) && !empty($theme_update_state->update) ){
        $update = $theme_update_state->update;
        $value->response[get_current_theme()] = array(
            'new_version' => $update->version,
            'url' => $update->url,
            'package' => $update->package
        );
    }
    return $value;	
} 
function deleteStoredData(){
	delete_option('update_chameleonS');
} 

当你添加搞定好就拥有了和WordPress默认主题更新功能啦,是不是很方便呢,好了今天的为自己的WordPress主题增加更新功能教程就写到这里了,如有不懂请加客服咨询。