1
當我使用wp_maybe_auto_update()
函數手動觸發WordPress自動更新時,自動更新過程完成後插件被取消激活。插件在WordPress自動插件更新後被取消激活
當我使用wp_maybe_auto_update()
函數手動觸發WordPress自動更新時,自動更新過程完成後插件被取消激活。插件在WordPress自動插件更新後被取消激活
我遇到了同樣的問題,並深入代碼揭示了原因。
Automatic_Upgrader
使用Plugin_Upgrader
來更新插件。插件升級程序有一個方法,deactivate_plugin_before_upgrade()
,用於在升級之前停用插件,但僅限於特定情況。它包含以下行:
// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
if (wp_doing_cron())
return $return;
因此,當更新由cron作業運行時,插件不會停用。自動更新通常是通過cron運行的,所以代碼已經假設他們總是會這樣做。如果自動更新在cron之外被觸發(例如手動調用wp_maybe_auto_update()
,則插件將被禁用,但它們不會自動重新激活)
一種解決方案是欺騙升級器,使其認爲cron在撥打電話wp_maybe_auto_update()
之前,通過掛接'wp_doing_cron'
篩選器運行:
add_filter('wp_doing_cron', '__return_true');
wp_maybe_auto_update();
remove_filter('wp_doing_cron', '__return_true');