Skip to content

Commit

Permalink
fix code bug, progress plugin code for updating table schema section
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleBlanchette committed Nov 6, 2024
1 parent 6a9dc89 commit 0df665f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ Then, update the `plugins_loaded` hook callback to point to a function that chec
add_action( 'plugins_loaded', 'wp_learn_custom_table_update_db_check' );
function wp_learn_custom_table_update_db_check() {
global $wp_learn_custom_table_db_version;
$current_custom_table_db_version = get_option( 'wp_learn_custom_table_db_version', '1.0' );
if ( version_compare( $wp_learn_current_custom_table_db_version, $wp_learn_custom_table_db_version ) ) {
$current_custom_table_db_version = get_option( 'wp_learn_custom_table_db_version', '1.0.0' );
if ( version_compare( $current_custom_table_db_version, $wp_learn_custom_table_db_version ) ) {
wp_learn_custom_table_create_submissions_table();
}
}
Expand All @@ -145,7 +145,7 @@ If the current version is lower than the default version, the table is updated.
Whenever the plugin's table schema is changed in the code, the version number should be increased so that the tables are checked and updated by the `dbDelta()` function.

```php
$wp_learn_custom_table_db_version = '1.0.1';
$wp_learn_custom_table_db_version = '1.1.0';

register_activation_hook( __FILE__, 'wp_learn_custom_table_create_submissions_table' );
function wp_learn_custom_table_create_submissions_table() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @package wp-learn-form-submissions
*/

$wp_learn_custom_table_db_version = '1.1.0';

// Only triggered when the plugin is activated, NOT when the plugin is updated.
register_activation_hook( __FILE__, 'wp_learn_custom_table_create_submissions_table' );
function wp_learn_custom_table_create_submissions_table() {
global $wpdb;
Expand All @@ -19,9 +22,23 @@ function wp_learn_custom_table_create_submissions_table() {
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
name tinytext NOT NULL,
message text NOT NULL,
url varchar(55) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) {$charset_collate};";

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );

// Set or update the currently installed version.
global $wp_learn_custom_table_db_version;
update_option( 'wp_learn_custom_table_db_version', $wp_learn_custom_table_db_version );
}

add_action( 'plugins_loaded', 'wp_learn_custom_table_update_db_check' );
function wp_learn_custom_table_update_db_check() {
global $wp_learn_custom_table_db_version;
$current_custom_table_db_version = get_option( 'wp_learn_custom_table_db_version', '1.0.0' );
if ( version_compare( $current_custom_table_db_version, $wp_learn_custom_table_db_version ) ) {
wp_learn_custom_table_create_submissions_table();
}
}

0 comments on commit 0df665f

Please sign in to comment.