由于WordPress 3.9,您可以使用upgrader_process_complete
挂钩
升级程序完成后(插件和主题更新),将触发此挂钩。
参见参考1, 2, 3
下面是一个示例代码:
<?php
/**
* Plugin Name: Test plugin 1
* Plugin URI: https://rundiz.com
* Description: A very simple plugin for testing. This plugin do nothing.
* Version: 0.1.8
* Author: Vee Winch
* Author URI: http://rundiz.com
* License: MIT
* License URI: https://opensource.org/licenses/MIT
* Text Domain: test-plugin1
* Domain Path:
*/
add_action(\'upgrader_process_complete\', \'testplugin_upgrade_completed\', 10, 2);
/**
* Upgrader process complete.
*
* @see \\WP_Upgrader::run() (wp-admin/includes/class-wp-upgrader.php)
* @param \\WP_Upgrader $upgrader_object
* @param array $hook_extra
*/
function testplugin_upgrade_completed(\\WP_Upgrader $upgrader_object, $hook_extra)
{
// get current plugin version. ( https://wordpress.stackexchange.com/a/18270/41315 )
if(!function_exists(\'get_plugin_data\')){
require_once(ABSPATH . \'wp-admin/includes/plugin.php\');
}
// https://developer.wordpress.org/reference/functions/get_plugin_data/
$plugin_data = get_plugin_data(__FILE__);
$plugin_version = ($plugin_data[\'Version\'] ?? \'unknown.version\');
unset($plugin_data);
if (
is_array($hook_extra) &&
array_key_exists(\'action\', $hook_extra) &&
$hook_extra[\'action\'] == \'update\'
) {
if (
array_key_exists(\'type\', $hook_extra) &&
$hook_extra[\'type\'] == \'plugin\'
) {
// if updated the plugins.
$this_plugin = plugin_basename(__FILE__);
$this_plugin_updated = false;
if (array_key_exists(\'plugins\', $hook_extra)) {
// if bulk plugin update (in update page)
foreach ($hook_extra[\'plugins\'] as $each_plugin) {
if ($each_plugin === $this_plugin) {
$this_plugin_updated = true;
break;
}
}// endforeach;
unset($each_plugin);
} elseif (array_key_exists(\'plugin\', $hook_extra)) {
// if normal plugin update or via auto update.
if ($this_plugin === $hook_extra[\'plugin\']) {
$this_plugin_updated = true;
}
}
if ($this_plugin_updated === true) {
// if this plugin is just updated.
// do your task here.
// DON\'T process anything from new version of code here, because it will work on old version of the plugin.
// please read again!! the code run here is not new (just updated) version but the version before that.
file_put_contents(WP_CONTENT_DIR . \'/test.txt\', \'v\'.$plugin_version."\\r\\n", FILE_APPEND);
// set transient to let it run later.
set_transient(\'testplugin_just_updated\', 1);
}
} elseif (
array_key_exists(\'type\', $hook_extra) &&
$hook_extra[\'type\'] == \'theme\'
) {
// if updated the themes.
// same as plugin, the bulk theme update will be set the name in $hook_extra[\'themes\'] as \'theme1\', \'theme2\'.
// normal update or via auto update will be set the name in $hook_extra[\'theme\'] as \'theme1\'.
}
}// endif; $hook_extra
}// testplugin_upgrade_completed
add_action(\'plugins_loaded\', \'testplugin_pluginloaded\');
/**
* Run once plugin loaded (on every page load).
*/
function testplugin_pluginloaded()
{
// get current plugin version. ( https://wordpress.stackexchange.com/a/18270/41315 )
if(!function_exists(\'get_plugin_data\')){
require_once(ABSPATH . \'wp-admin/includes/plugin.php\');
}
// https://developer.wordpress.org/reference/functions/get_plugin_data/
$plugin_data = get_plugin_data(__FILE__);
$plugin_version = ($plugin_data[\'Version\'] ?? \'unknown.version\');
unset($plugin_data);
if (get_transient(\'testplugin_just_updated\') && current_user_can(\'manage_options\')) {
// if it was marked in transient that this plugin just updated and current user is admin.
// you can use new verion of code here.
file_put_contents(WP_CONTENT_DIR . \'/test-update-by-transient.txt\', \'v\'.$plugin_version."\\r\\n", FILE_APPEND);
// your update code here.
// delete transient when done to do not let this code run again.
delete_transient(\'testplugin_just_updated\');
}
}// testplugin_pluginloaded
The
upgrader_process_complete
当插件/主题更新时,钩子将与当前版本的代码一起运行。不使用新版本。
您有插件版本1.0,您可以运行更新页面或自动更新将下载并提取插件版本2.0。这个upgrader_process_complete
将调用挂钩您的插件版本1.0将在中运行upgrader_process_complete
挂钩完成后,重新加载页面和plugins_loaded
将调用挂钩您的插件版本2.0在plugins_loaded
钩(必须激活插件。)我之前发布的代码中已经解释了这些内容(编辑之前),但可能不清楚或很难看到。
这个upgrader_process_complete
为此创建挂钩(请阅读参考链接3). 在升级完成后运行。
您可以使用plugins_loaded
用接受答案中的代码钩住。它确实有用,而且写得更短,或者你可能有更好的想法来使用它upgrader_process_complete
钩
这个upgrader_process_complete
吊钩将在以下情况下工作:
通过更新页面进行更新通过插件或主题页面更新通过自动更新或WP Cron更新当您通过FTP更新插件或主题时,上面的代码不起作用,因为它无法检测到瞬态选项。在这种情况下,接受的答案是您唯一的最佳选择。