Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mahfelwp/b8092f1962562cd8b01f07b5a87133e2 to your computer and use it in GitHub Desktop.
Save mahfelwp/b8092f1962562cd8b01f07b5a87133e2 to your computer and use it in GitHub Desktop.
Add new column to plugin table in new version
<?php
final class Plugin_Name {
private function __construct() {
register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
}
public function plugin_activation() {
if ( get_option( 'plugin_version' ) != PLUGIN_VERSION ) {
update_option( 'plugin_version', PLUGIN_VERSION );
}
$this->create_tables();
$this->upgrade_tables();
}
public function create_tables() {
// codes here...
}
public function upgrade_tables() {
if ( get_option( 'plugin_version' ) == '1.2.0' ) {
global $wpdb, $table_prefix;
$plugin_tbl_name = $table_prefix . 'plugin_tbl_name';
$the_upgrade_query = "ALTER TABLE `{$plugin_tbl_name}` ADD `products` TEXT NULL DEFAULT NULL AFTER `users`;";
$wpdb->query( $the_upgrade_query );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment