Advertisement
amansdpr

Create/Delete Table after activating/deactivate WP theme.

Feb 5th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2.  
  3. //=========================================================================================================
  4. //  Create Table after activating theme
  5. //=========================================================================================================
  6.    
  7. global $cb_db_version;
  8. $cb_db_version = '1.1';
  9.  
  10.    
  11.     $prefix = $wpdb->prefix;
  12.    
  13.     $table_name1 = $prefix . 'table_one';
  14.     $table_name2 = $prefix . 'table_two';
  15.     $table_name3 = $prefix . 'table_three';
  16.    
  17. function cb_create_table() {
  18.     global $wpdb;
  19.     global $jal_db_version;
  20.     $charset_collate = $wpdb->get_charset_collate();
  21.  
  22.     global $table_name1, $table_name2, $table_name3;
  23.  
  24.     $sql = "CREATE TABLE $table_name1 (
  25.           `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  26.           `description` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  27.           `rotator_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Series',
  28.           PRIMARY KEY (`id`)
  29.         ) $charset_collate;";
  30.  
  31.     $sql .= "CREATE TABLE $table_name2 (
  32.           `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  33.           `click_rotators_id` int(10) unsigned NOT NULL DEFAULT '0',
  34.           `series_position` int(10) unsigned NOT NULL DEFAULT '0',
  35.           PRIMARY KEY (`id`)
  36.         ) $charset_collate;";
  37.  
  38.     $sql .= "CREATE TABLE $table_name3 (
  39.           `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  40.           `click_rotators_id` int(10) unsigned NOT NULL DEFAULT '0',
  41.           `click_rotator_links_id` int(10) unsigned NOT NULL DEFAULT '0',
  42.           PRIMARY KEY (`id`)
  43.         ) $charset_collate;";
  44.  
  45.     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  46.     dbDelta( $sql );
  47.  
  48.     add_option( 'cb_db_version', $jal_db_version );
  49. }
  50.  
  51.  
  52.  
  53. // Add table after Installing and Activating theme
  54. function cb_update_table() {
  55.     global $cb_db_version;
  56.     if ( get_site_option( 'cb_db_version' ) != $cb_db_version ) {
  57.         cb_create_table();
  58.     }
  59. }
  60. add_action( 'after_switch_theme', 'cb_update_table' );
  61.  
  62.  
  63.  
  64. //=========================================================================================================
  65. //  Delete Table after deactivating theme
  66. //=========================================================================================================
  67.  
  68. function theme_deactivation_function(){
  69.     global $wpdb;
  70.     global $cb_db_version;
  71.     global $table_name1, $table_name2, $table_name3;
  72.     $sql = "DROP TABLE $table_name1, $table_name2, $table_name3;";    
  73.     $wpdb->query($sql);
  74.     delete_option($cb_db_version); 
  75. }
  76. add_action('switch_theme', 'theme_deactivation_function');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement