Last active
December 11, 2015 02:58
-
-
Save wpmark/4534059 to your computer and use it in GitHub Desktop.
Adding a Custom table in WordPress using a WordPress plugin.
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 | |
function wpct_create_db_table() { | |
global $wpdb; | |
/* create the table name using the wordpress table prefix for this site */ | |
$wpct_tablename = $wpdb->prefix . "wpct"; | |
/* setup the structure of the table creating these as a variable */ | |
$wpct_sql = "CREATE TABLE IF NOT EXISTS $wpct_tablename ( | |
wpct_id int(11) NOT NULL AUTO_INCREMENT, | |
wpct_post_id varchar(100) NOT NULL, | |
wpct_user_id varchar(100) NOT NULL, | |
wpct_username varchar(100) NOT NULL, | |
wpct_date datetime, | |
wpct_ip varchar(100) NOT NULL, | |
PRIMARY KEY (wpct_id) | |
);"; | |
/* include dbdelta stuff */ | |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
/* build the table using the variables above */ | |
dbDelta( $wpct_sql ); | |
} | |
/* run the table creation function only on plugin activation */ | |
register_activation_hook( __FILE__, 'wpct_create_db_table' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment