Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kingjmaningo/c4e1a35767ef4e6ac24fee72fd09d8b4 to your computer and use it in GitHub Desktop.
Save kingjmaningo/c4e1a35767ef4e6ac24fee72fd09d8b4 to your computer and use it in GitHub Desktop.
Create database table upon plugin activation
<?php
register_activation_hook( __FILE__, array( 'SAMPLE_CLASS', 'plugin_activated' ) );
class SAMPLE_CLASS {
public function plugin_activated() {
$add_custom_table = $wpdb->prefix . 'table_name';
if($wpdb->get_var("SHOW TABLES LIKE '$add_custom_table'") != $add_custom_table) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $add_custom_table (
`id` int(100) NOT NULL auto_increment,
`field_label` int(100) NOT NULL,
`field_name` VARCHAR(255) NOT NULL,
UNIQUE KEY `id` (`id`)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment