/inc/db-settings.php

  1. <?php 
  2. // db table version 
  3. global $db_table_version; 
  4. $db_table_version = "1.0"; 
  5.  
  6. function install() { 
  7. // makes the location table 
  8. global $wpdb; 
  9. global $db_table_version; 
  10.  
  11. $table_name = $wpdb->prefix . "gmt_tab"; 
  12.  
  13. // provide an update 
  14. $installed_ver = get_option( "db_table_version" ); 
  15. if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name || $installed_ver != $db_table_version ) { 
  16. $sql = "CREATE TABLE " . $table_name . " ( 
  17. `id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,  
  18. `lat` TEXT NOT NULL,  
  19. `lon` TEXT NOT NULL,  
  20. `title` TEXT NOT NULL,  
  21. `content` TEXT NOT NULL,  
  22. `zoom` TEXT NOT NULL,  
  23. UNIQUE KEY id (id) 
  24. )ENGINE=InnoDB DEFAULT CHARSET=utf8;"; 
  25.  
  26. $sql.= "INSERT INTO `" . $table_name . "` (`id`, `lat`, `lon`, `title`, `content`, `zoom`) VALUES 
  27. (1, '52.1', '11.3', 'Title A', 'Lorem Ipsum..', '12'),  
  28. (2, '51.2', '22.2', 'Title B', 'Lorem Ipsum..', '12'),  
  29. (3, '49.4', '35.9', 'Title C', 'Lorem Ipsum..', '12'),  
  30. (4, '47.8', '15.6', 'Title D', 'Lorem Ipsum..', '12');"; 
  31.  
  32. require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
  33. dbDelta($sql); 
  34.  
  35. update_option( "db_table_version", $db_table_version ); 
  36. add_option("db_table_version", $db_table_version); 
  37.  
  38. function install_data() { 
  39. global $wpdb; 
  40. $welcome_name = "Mr. WordPress"; 
  41. $welcome_text = "Congratulations, you just completed the installation!"; 
  42. $table_name = $wpdb->prefix . "gmt_tab"; 
  43. $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $welcome_name, 'text' =>  
  44. $welcome_text ) ); 
  45.  
  46. // check for update 
  47. add_action( 'plugins_loaded', 'update_db_check' ); 
  48. function update_db_check() { 
  49. global $db_table_version; 
  50. if ( get_site_option( 'db_table_version' ) != $db_table_version ) { 
  51. install(); 
.