Skip to content

Instantly share code, notes, and snippets.

Last active Dec 11, 2015
What would you like to do?
Adding a Custom table in WordPress using a WordPress plugin.
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