令人惊讶的是,事实并非如此。相反,您需要使用以下函数,该函数在插件激活时运行。
define( \'YOUR_PLUGIN_VERSION\', \'1.0.0\' );
register_activation_hook( __FILE__, \'your_plugin_activation_function\' );
function your_plugin_activation_function() {
// Do activation tasks here.
your_install_function();
your_upgrade_migration_function();
}
运行安装脚本。
function your_install_function() {
// Set the current version of the plugin in the db.
update_option( \'your_plugin_version\', YOUR_PLUGIN_VERSION );
}
然后对每个新版本进行比较,基本上执行数据库迁移等操作。
function your_upgrade_migration_function() {
// Using a version prior to 1.1.1
if ( version_compare( YOUR_PLUGIN_VERSION, \'1.1.1\', \'<\' ) ) {
// Do upgrade things unique for this version.
}
// Using a version prior to 1.2.0
if ( version_compare( YOUR_PLUGIN_VERSION, \'1.2.0\', \'<\' ) ) {
// Do upgrade things unique for this version.
}
}