Last active
December 14, 2015 19:29
-
-
Save ericjuden/5137082 to your computer and use it in GitHub Desktop.
WordPress Custom Tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Create Sample Tables Plugin | |
Plugin URI: http://ericjuden.com | |
Description: Create some dummy tables | |
Version: 1.0 | |
Author: ericjuden | |
Author URI: http://ericjuden.com | |
*/ | |
class Create_Sample_Tables { | |
var $db_version = '1.0'; | |
function __construct(){ | |
register_activation_hook(__FILE__, array($this, 'activate')); | |
} | |
function activate(){ | |
require_once(ABSPATH . '/wp-admin/includes/upgrade.php'); | |
global $wpdb; | |
$current_db_version = get_option('sample_tables_db_version', ''); | |
switch($current_db_version){ | |
case '': // New install...run for the first time | |
$table_name = $wpdb->prefix . 'sampletables_widget'; | |
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name){ // Does table exist | |
$sql = "CREATE TABLE " . $table_name . " ( | |
widget_id INT NOT NULL AUTO_INCREMENT, | |
name VARCHAR(100) NOT NULL, | |
status VARCHAR(100) NOT NULL, | |
PRIMARY KEY (widget_id), | |
UNIQUE KEY widget_id (widget_id) | |
);"; | |
dbDelta($sql); | |
// Add some test data | |
$data = array( | |
'name' => 'Widget 1', | |
'status' => 'testing' | |
); | |
$data_format = array('%s', '%s'); | |
$wpdb->insert($table_name, $data, $data_format); | |
} | |
$table_name = $wpdb->prefix . 'sampletables_widget2'; | |
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name){ // Does table exist | |
// do the same thing again | |
} | |
break; | |
case '1.0': // Currently 1.0 - upgrade to next release | |
break; | |
} | |
add_option('sample_tables_db_version', $this->db_version); | |
} | |
} | |
$create_sample_tables = new Create_Sample_Tables(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $wpdb; | |
if(!defined('WP_UNINSTALL_PLUGIN')){ | |
exit(); | |
} | |
delete_option('sample_tables_db_version'); | |
$sql = "DROP TABLE IF EXISTS " . $wpdb->prefix . "sampletables_widget"; | |
$results = $wpdb->query($sql); | |
$sql = "DROP TABLE IF EXIST " . $wpdb->prefix . "sampletables_widget2"; | |
$results = $wpdb->query($sql); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment