0
0
Wordpressframework~10 mins

Plugin database tables in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Plugin database tables
Plugin Activation
Check if DB Tables Exist?
YesSkip Creation
No
Create Plugin DB Tables
Store Table Names & Versions
Plugin Uses Tables for Data Storage
Plugin Deactivation or Update
Optional: Update or Remove Tables
When a plugin activates, it checks if its database tables exist. If not, it creates them, stores info, and uses them for data. On updates or deactivation, it may update or remove tables.
Execution Sample
Wordpress
<?php
function plugin_activate() {
  global $wpdb;
  $table_name = $wpdb->prefix . 'myplugin_data';
  if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table_name (id mediumint(9) NOT NULL AUTO_INCREMENT, data text NOT NULL, PRIMARY KEY  (id)) $charset_collate;";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
    add_option('myplugin_db_version', '1.0');
  }
}
This code runs on plugin activation to create a custom database table if it doesn't exist.
Execution Table
StepActionCheckResultNext Step
1Plugin activatesN/AStart activationCheck if table exists
2Check table existenceSHOW TABLES LIKE 'wp_myplugin_data'Table not foundCreate table
3Create table SQL preparedN/ASQL ready with charsetRun dbDelta()
4Run dbDelta()Execute CREATE TABLETable created successfullyStore version info
5Store version infoN/AVersion saved in optionsActivation complete
6Activation completeN/APlugin ready to use tablesEnd
💡 Table exists or created, activation finishes successfully
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
$table_nameundefinedwp_myplugin_datawp_myplugin_datawp_myplugin_datawp_myplugin_data
$charset_collateundefinedundefinedutf8mb4_unicode_ciutf8mb4_unicode_ciutf8mb4_unicode_ci
$sqlundefinedundefinedCREATE TABLE SQL stringCREATE TABLE SQL stringCREATE TABLE SQL string
Key Moments - 3 Insights
Why do we check if the table exists before creating it?
To avoid errors and duplicate tables, the plugin checks if the table already exists (see execution_table step 2). If it exists, creation is skipped.
What does dbDelta() do with the SQL?
dbDelta() runs the SQL safely, creating or updating the table structure without losing data (see execution_table step 4).
Why store the table version after creation?
Storing the version helps the plugin know if it needs to update the table structure later (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 2?
AThe plugin checks if the database table exists
BThe plugin creates the database table
CThe plugin stores the version info
DThe plugin finishes activation
💡 Hint
Refer to execution_table row with Step 2 describing the check for table existence
According to variable_tracker, what value does $charset_collate have after step 3?
Aundefined
Butf8mb4_unicode_ci
Cwp_myplugin_data
DCREATE TABLE SQL string
💡 Hint
Check variable_tracker row for $charset_collate at After Step 3 column
If the table already exists, which step in execution_table is skipped?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table step 2 result 'Table not found' triggers creation at step 4; if table exists, creation (step 4) is skipped
Concept Snapshot
Plugin database tables:
- On activation, check if tables exist
- If not, create tables with dbDelta()
- Store version info for updates
- Use $wpdb->prefix for table names
- Use charset collate for compatibility
- Avoid duplicate tables by checking first
Full Transcript
When a WordPress plugin activates, it needs to store data in custom database tables. The plugin first checks if its tables already exist using a SQL query. If the tables do not exist, it prepares a CREATE TABLE SQL statement including the correct character set and collation. Then it runs the dbDelta() function to create or update the table safely. After creation, the plugin stores the table version in the WordPress options to track future updates. This process prevents errors from duplicate tables and ensures the plugin can manage its data storage properly. Variables like $table_name hold the full table name with prefix, and $charset_collate ensures the table uses the right encoding. This flow is essential for plugins that need custom data storage beyond WordPress default tables.