WP更新插件数据库表



我正在构建一个插件,该插件在激活和停用时将创建和删除数据库,这工作正常。

问题是当我尝试为数据库表创建更新时,它会出错并中断。

我有一个名为类WIP_Forecast的类文件,下面是我的类中的更新函数。

我对插件构建很陌生,真的很想得到一些帮助来解决这个问题。

global $wip_forecast_db_version, $current_version;
$wip_forecast_db_version = '1.1';
$get_current_version = get_option('wip_forecast_db_version');
class WIP_Forecast
{
public function __construct()
{
// register actions
add_action( 'plugins_loaded', 'update_db_checker' );            
}
public static function activate(){
// Works
}
....
/**
* Update Plugin DB
*/
function update_db_checker()
{
if ( $get_current_version != $wip_forecast_db_version ) {
wip_forecast_updates($wip_forecast_db_version);
}
}
function wip_forecast_updates($wip_forecast_db_version)
{           
global $wpdb;
$table_name = $wpdb->prefix . 'wip_forecast';
if ($get_current_version === 1.1)
{
$wpdb->query("ALTER TABLE $table_name ADD COLUMN `count` SMALLINT(6) NOT NULL");
} else if ($get_current_version === 1.2){
//$wpdb->query("ALTER TABLE $table_name ADD COLUMN `count` SMALLINT(6) NOT NULL");
}else{
//Do Nothing
}
update_option('wip_forecast_db_version', $wip_forecast_db_version);
}
...
public static function deactivate(){
// Works
}
}
if(class_exists('WIP_Forecast'))
{
// Installation and uninstallation hooks
register_activation_hook(__FILE__, array('WIP_Forecast', 'activate'));
register_activation_hook( __FILE__, 'wip_forecast_create_db' );
register_deactivation_hook(__FILE__, array('WIP_Forecast', 'deactivate'));
// instantiate the plugin class
$wip_forecast = new WIP_Forecast();
}
global $wip_forecast_db_version, $get_current_version;
class WIP_Forecast
{
private $wip_forecast_db_version;
private $get_current_version;
public function __construct()
{
$wip_forecast_db_version = '1.1';
$get_current_version = get_option('wip_forecast_db_version');
// register actions
add_action( 'plugins_loaded', 'update_db_checker' );
}
}

最新更新