Created
March 3, 2021 06:47
-
-
Save kingjmaningo/c4e1a35767ef4e6ac24fee72fd09d8b4 to your computer and use it in GitHub Desktop.
Create database table upon plugin activation
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 | |
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