Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Created March 16, 2020 17:56
Show Gist options
  • Save bhubbard/7b770589308c78e918541bda9e25d811 to your computer and use it in GitHub Desktop.
Save bhubbard/7b770589308c78e918541bda9e25d811 to your computer and use it in GitHub Desktop.
A better example of an upgrade file.
<?php
/* Exit if accessed directly. */
defined( 'ABSPATH' ) || exit;
// Require WordPress Upgrade.
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
function upgrade_v2() {
global $wpdb;
$current_version = get_option( 'my_plugin_version', '1.0' ) ?? '';
if ( version_compare( $current_version, '2.0', '<' ) ) {
// $charset_collate = $wpdb->get_charset_collate();
// Example: Add Clean Index.
// add_clean_index( $wpdb->posts, 'post_name' );
// drop_index( $table, $index );
// Maybe Add, Drop or Check Column.
// maybe_add_column( $table_name, $column_name, $create_ddl );
// maybe_drop_column( $table_name, $column_name, $drop_ddl );
// check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null );
// Maybe Create Table.
// maybe_create_table( $table_name, $create_ddl );
// Maybe Convert Table to UTF8MB4.
// maybe_convert_table_to_utf8mb4( $table );
// delete_option( string $option );
// delete_transient( string $transient );
// Flush Rewrites & Clear Cache.
$flush_rules = flush_rewrite_rules() ?? '';
$cache_flush = wp_cache_flush() ?? '';
$updated_version = update_option( 'my_plugin_version', '2.0' ) ?? '';
$results = array(
'current_version' = $current_version,
'updated_version' = $updated_version,
'actions' => array(
'flush_rewrite_rules' = $flush_rules,
'cache_flush' = $cache_flush
)
);
return $results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment