0
0
Wordpressframework~20 mins

Plugin database tables in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Plugin Database Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does a WordPress plugin typically create its database tables?

When a WordPress plugin needs to store custom data, it often creates its own database tables. How does the plugin usually create these tables?

ABy running SQL commands directly inside the plugin's main file without any checks.
BBy modifying the WordPress core database files manually during plugin activation.
CBy using the <code>register_activation_hook</code> to run a function that creates tables with <code>dbDelta()</code> after checking the table doesn't exist.
DBy creating tables only when the plugin's admin page is loaded for the first time.
Attempts:
2 left
💡 Hint

Think about how plugins safely create tables only once during activation.

📝 Syntax
intermediate
2:00remaining
Identify the correct SQL syntax for creating a plugin table with charset in WordPress

Which SQL statement correctly creates a plugin table with the proper charset and collation in WordPress?

ACREATE TABLE wp_myplugin_data (id INT NOT NULL) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
BCREATE TABLE wp_myplugin_data (id INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CCREATE TABLE wp_myplugin_data (id INT NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
DCREATE TABLE wp_myplugin_data (id INT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
Attempts:
2 left
💡 Hint

WordPress recommends using InnoDB engine and utf8mb4 charset for compatibility.

🔧 Debug
advanced
2:00remaining
Why does this plugin table creation code fail to create the table?

Consider this plugin activation code snippet:

global $wpdb;
$table_name = $wpdb->prefix . 'myplugin';
$sql = "CREATE TABLE $table_name (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  name varchar(255) NOT NULL,
  PRIMARY KEY  (id)
)";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

Why might the table not be created?

AThe SQL statement has a syntax error: the closing parenthesis is followed by a semicolon inside the string.
BThe SQL statement is missing the charset and collation specification, so dbDelta ignores it.
CThe <code>dbDelta</code> function requires the SQL string to have the <code>ENGINE=InnoDB</code> clause to create tables.
DThe <code>dbDelta</code> function only works if the table name is lowercase without prefix.
Attempts:
2 left
💡 Hint

Check the SQL syntax carefully, especially punctuation inside the string.

state_output
advanced
2:00remaining
What is the value of $table_version after plugin activation with version check?

A plugin stores its database version in the option myplugin_db_version. On activation, it runs this code:

global $wpdb;
$table_version = get_option('myplugin_db_version');
if ( $table_version != '1.0' ) {
  // create or update tables
  update_option('myplugin_db_version', '1.0');
}

If the option myplugin_db_version does not exist before activation, what will be its value after activation?

Anull
B'' (empty string)
Cfalse
D'1.0'
Attempts:
2 left
💡 Hint

Think about what update_option does after the check.

🧠 Conceptual
expert
3:00remaining
Which approach ensures plugin database tables are updated safely on plugin upgrade?

When a plugin updates to a new version that changes the database schema, what is the best practice to update the plugin's database tables safely?

AUse a version number stored in options to detect schema changes and run incremental <code>dbDelta()</code> calls during admin_init hook.
BDrop the existing tables and recreate them from scratch during plugin activation.
CManually run ALTER TABLE SQL commands without checking the current schema version.
DRequire users to uninstall and reinstall the plugin to update the database tables.
Attempts:
2 left
💡 Hint

Think about how to avoid data loss and keep schema in sync.