diff --git a/plugin-developer-learning-pathway/07-Custom-Database-Tables/01-Creating-and-Managing-Custom-Tables.md b/plugin-developer-learning-pathway/07-Custom-Database-Tables/01-Creating-and-Managing-Custom-Tables.md index 4fb96be..8503037 100644 --- a/plugin-developer-learning-pathway/07-Custom-Database-Tables/01-Creating-and-Managing-Custom-Tables.md +++ b/plugin-developer-learning-pathway/07-Custom-Database-Tables/01-Creating-and-Managing-Custom-Tables.md @@ -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(); } } @@ -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() { diff --git a/plugin-developer-learning-pathway/07-Custom-Database-Tables/wpl-form-submissions.php b/plugin-developer-learning-pathway/07-Custom-Database-Tables/wpl-form-submissions.php index 5d2d31b..add09e7 100644 --- a/plugin-developer-learning-pathway/07-Custom-Database-Tables/wpl-form-submissions.php +++ b/plugin-developer-learning-pathway/07-Custom-Database-Tables/wpl-form-submissions.php @@ -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; @@ -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(); + } }