Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
WordPress Plugin Development

Creating Custom Database Tables

Create custom tables with dbDelta on plugin activation when CPT/meta is not enough.

Creating Custom Database Tables — a practical guide to WordPress dbDelta custom table with clear examples you can reuse in real projects.

WordPress Plugin Development Series (29/95). Prefer one article? Read the complete WordPress plugin development tutorial.

Short description

Use `dbDelta` carefully (exact formatting matters). Store a DB version option for upgrades.

dbDelta create

global $wpdb;
$table = $wpdb->prefix . 'acme_items';
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  title VARCHAR(200) NOT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY  (id)
) $charset;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);

Leave a reply

Your email address will not be published. Required fields are marked *