一般我們開發(fā)的WordPress免費(fèi)插件都會(huì)提交到WordPress官方網(wǎng)站,以便實(shí)現(xiàn)在線安裝和一鍵更新。但如果我們開發(fā)的是WordPress收費(fèi)插件,或者插件無法提交到WordPress官方,那就只能自托管了,這個(gè)時(shí)候,如何為插件添加在線更新功能呢?
今天,倡萌分享的是來自@Misha Rudrastyh的教程,希望可以幫助到有需要的朋友。
第1步:在服務(wù)器上創(chuàng)建具有更新信息和插件zip壓縮包的.json文件
WordPress 的更新API接受某些參數(shù),例如插件名稱、作者、更新歷史等。您可以:
- 在您的插件文件中直接指定其中的一些靜態(tài)文件(插件作者、插件名稱..),并從更新服務(wù)器獲取其他文件(下載網(wǎng)址、新版本、上次更新)。
- 或者,您可以從更新服務(wù)器中獲取所有內(nèi)容。
在本教程中,我將向您展示第一種方法,因?yàn)樗忧逦锥?/p>
首先,您必須在服務(wù)器上的某個(gè)位置創(chuàng)建一個(gè).json文件,例如YOUR_WEBSITE/info.json。然后這個(gè)文件應(yīng)該包含下面的相關(guān)參數(shù):
{
"version" => "1.1",
"download_url" : "https://rudrastyh.com/misha-plugin.zip",
"requires" : "3.0",
"tested" : "4.8.1",
"requires_php" : "5.3",
"last_updated" : "2017-08-17 02:10:00",
"sections" : {
"description" : "This is the plugin to test your updater script",
"installation" : "Upload the plugin to your blog, Activate it, that's it!",
"changelog" : "<h4>1.1 – January 17, 2020</h4><ul><li>Some bugs are fixed.</li><li>Release date.</li></ul>"
},
"banners" : {
"low" : "https://YOUR_WEBSITE/banner-772x250.jpg",
"high" : "https://YOUR_WEBSITE/banner-1544x500.jpg"
},
"screenshots" : "<ol><li><a href='IMG_URL' target='_blank'><img src='IMG_URL' alt='CAPTION' /></a><p>CAPTION</p></li></ol>"
}
還有很多其他參數(shù),我將在下面介紹所有這些參數(shù)。您可以選擇在info.json文件中指定哪個(gè)參數(shù)。
并且我們需要注意下zip插件包的文件結(jié)構(gòu):

正確的做法是連同插件文件夾一起壓縮,而不是僅僅壓縮插件文件夾內(nèi)部的文件。否則,當(dāng)點(diǎn)擊“更新”按鈕后,舊版本的插件將被刪除,但新版本插件并不會(huì)生效,因?yàn)閃ordPress會(huì)隨機(jī)生成類似于misha-plugin-xhfuif的插件文件夾,并且將返回錯(cuò)誤:“由于錯(cuò)誤,misha-plugin.php已被停用:插件文件不存在。”
第2步:自定義插件彈窗信息
當(dāng)您單擊“查看詳細(xì)信息”時(shí),WordPress會(huì)從WordPress的服務(wù)器顯示相應(yīng)的插件信息。最有趣的是,該信息可以通過鉤子定制,并在WordPress連接到它自己的服務(wù)器之前生效。 因此,它不會(huì)影響網(wǎng)站的性能。

相關(guān)的代碼如下:
add_filter('plugins_api', 'misha_plugin_info', 20, 3);
/*
* $res empty at this step
* $action 'plugin_information'
* $args stdClass Object ( [slug] => woocommerce [is_ssl] => [fields] => Array ( [banners] => 1 [reviews] => 1 [downloaded] => [active_installs] => 1 ) [per_page] => 24 [locale] => en_US )
*/
function misha_plugin_info( $res, $action, $args ){
// 如果沒有執(zhí)行獲取插件信息的操作,則不往下執(zhí)行
if( 'plugin_information' !== $action ) {
return false;
}
$plugin_slug = 'YOUR PLUGIN SLUG'; //插件的slug別名,會(huì)在下面的代碼中多次用到
// 如果不是我們這個(gè)插件,不往下執(zhí)行
if( $plugin_slug !== $args->slug ) {
return false;
}
// 嘗試先通過緩存獲取數(shù)據(jù)
if( false == $remote = get_transient( 'misha_update_' . $plugin_slug ) ) {
// info.json 是保存在你的服務(wù)器的文件
$remote = wp_remote_get( 'https://rudrastyh.com/wp-content/uploads/info.json', array(
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json'
) )
);
if ( ! is_wp_error( $remote ) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && ! empty( $remote['body'] ) ) {
set_transient( 'misha_update_' . $plugin_slug, $remote, 43200 ); // 設(shè)置緩存12個(gè)小時(shí)
}
}
if( ! is_wp_error( $remote ) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && ! empty( $remote['body'] ) ) {
$remote = json_decode( $remote['body'] );
$res = new stdClass();
$res->name = $remote->name;
$res->slug = $plugin_slug;
$res->version = $remote->version;
$res->tested = $remote->tested;
$res->requires = $remote->requires;
$res->author = '<a >Misha Rudrastyh</a>';
$res->author_profile = 'https://profiles.wordpress.org/rudrastyh';
$res->download_link = $remote->download_url;
$res->trunk = $remote->download_url;
$res->requires_php = '5.3';
$res->last_updated = $remote->last_updated;
$res->sections = array(
'description' => $remote->sections->description,
'installation' => $remote->sections->installation,
'changelog' => $remote->sections->changelog
// 你還可以在這里添加自定義的選項(xiàng)卡
);
// 假如你需要添加縮略圖部分,請按照下面的格式添加圖片內(nèi)容
// <ol><li><a href="IMG_URL" target="_blank"><img src="IMG_URL" alt="CAPTION" /></a><p>CAPTION</p></li></ol>
if( !empty( $remote->sections->screenshots ) ) {
$res->sections['screenshots'] = $remote->sections->screenshots;
}
$res->banners = array(
'low' => 'https://YOUR_WEBSITE/banner-772x250.jpg',
'high' => 'https://YOUR_WEBSITE/banner-1544x500.jpg'
);
return $res;
}
return false;
}
接下來簡單描述下一些上面沒有提及的參數(shù):
contributors:貢獻(xiàn)者(contributors)字段信息為wordpress.org個(gè)人資料名稱和網(wǎng)址的關(guān)聯(lián)數(shù)組,例如
array(
'rudrastyh' => 'https://profiles.wordpress.org/rudrastyh',
'contributer' => 'https://profiles.wordpress.org/contributer'
)

requires_php:最低PHP版本要求
rating:評分,從 1 到 100
ratings:5星評價(jià)中各個(gè)星級的得分次數(shù)
array(
5 => 2104
4 => 116,
3 => 64,
2 => 57,
1 => 175
)
num_ratings:所有評分的總數(shù)

support_threads:相關(guān)的論壇文章數(shù)
support_threads_resolved:已解決的論壇文章數(shù)
active_installs:插件的安裝數(shù)量
added:添加日期,YYYY-MM-DD格式的日期
homepage:插件的主頁網(wǎng)址
reviews:插件的點(diǎn)評,例如

versions:插件各個(gè)版本以及對應(yīng)的下載網(wǎng)址數(shù)組,例如
array(
'1.0' => 'URL of zip archive of 1.0 plugin version',
'trunk' => 'URL of the latest plugin version'
)
donate_link:捐助鏈接
第3步:將更新信息推送到WP Transients 中

add_filter('site_transient_update_plugins', 'misha_push_update' );
function misha_push_update( $transient ){
if ( empty($transient->checked ) ) {
return $transient;
}
$plugin_slug = 'YOUR PLUGIN SLUG'; //插件的slug別名,會(huì)在下面的代碼中多次用到
// 嘗試先從緩存中獲取信息,如果要禁用緩存,請注銷 12,22,23,24,26 行的代碼
if( false == $remote = get_transient( 'misha_upgrade_' . $plugin_slug ) ) {
// info.json 是在你自己的服務(wù)器上的文件
$remote = wp_remote_get( 'https://YOUR_WEBSITE/SOME_PATH/info.json', array(
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json'
) )
);
if ( !is_wp_error( $remote ) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && !empty( $remote['body'] ) ) {
set_transient( 'misha_upgrade_' . $plugin_slug, $remote, 43200 ); // 設(shè)置緩存12個(gè)小時(shí)
}
}
if( $remote ) {
$remote = json_decode( $remote['body'] );
// 您安裝的插件版本應(yīng)該在下行代碼中!你也可以動(dòng)態(tài)獲取它
if( $remote && version_compare( '1.0', $remote->version, '<' ) && version_compare($remote->requires, get_bloginfo('version'), '<' ) ) {
$res = new stdClass();
$res->slug = $plugin_slug;
$res->plugin = 'YOUR_PLUGIN_FOLDER/YOUR_PLUGIN_SLUG.php'; // 如果您的插件沒有自己的目錄,應(yīng)該只填寫YOUR_PLUGIN_SLUG.php
$res->new_version = $remote->version;
$res->tested = $remote->tested;
$res->package = $remote->download_url;
$transient->response[$res->plugin] = $res;
//$transient->checked[$res->plugin] = $remote->version;
}
}
return $transient;
}
第4步:更新后清除緩存
最后一步是在更新插件后清除緩存。
add_action( 'upgrader_process_complete', 'misha_after_update', 10, 2 );
function misha_after_update( $upgrader_object, $options ) {
if ( $options['action'] == 'update' && $options['type'] === 'plugin' ) {
// 僅在新插件版本已安裝時(shí)清除緩存
delete_transient( 'misha_upgrade_YOUR_PLUGIN_SLUG' );
}
}
以上就是本文的所有內(nèi)容,你還可以繼續(xù)看該作者的《WordPress更新插件時(shí)檢查授權(quán)許可》,再次感謝@Misha Rudrastyh。
推薦閱讀:




