0
0
Wordpressframework~10 mins

Plugin database tables in Wordpress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the WordPress database global object.

Wordpress
<?php global [1]; ?>
Drag options to blanks, or click blank then click option'
A$post
B$plugin
C$user
D$wpdb
Attempts:
3 left
💡 Hint
Common Mistakes
Using $post instead of $wpdb
Forgetting the dollar sign
Using $plugin which is unrelated
2fill in blank
medium

Complete the code to create a new table name with the WordPress prefix.

Wordpress
$table_name = [1] . 'my_plugin_table';
Drag options to blanks, or click blank then click option'
A$wpdb->name
B$wpdb->suffix
C$wpdb->prefix
D$wpdb->table
Attempts:
3 left
💡 Hint
Common Mistakes
Using $wpdb->suffix which does not exist
Using $wpdb->name which is not the prefix
Using $wpdb->table which is invalid
3fill in blank
hard

Fix the error in the SQL query to create a table with the correct charset collate.

Wordpress
$charset_collate = [1];
$sql = "CREATE TABLE $table_name (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  name varchar(255) NOT NULL,
  PRIMARY KEY  (id)
) $charset_collate;";
Drag options to blanks, or click blank then click option'
A$wpdb->charset . ' COLLATE ' . $wpdb->collate
B$wpdb->charset . ' COLLATE=' . $wpdb->collate
C$wpdb->charset . ' ' . $wpdb->collate
D$wpdb->charset . ' COLLATE' . $wpdb->collate
Attempts:
3 left
💡 Hint
Common Mistakes
Missing spaces around 'COLLATE'
Using '=' sign incorrectly
Concatenating without spaces
4fill in blank
hard

Fill both blanks to prepare and execute a safe SQL query to insert data into the plugin table.

Wordpress
$wpdb->[1](
  $table_name,
  array('name' => $name),
  array([2] => '%s')
);
Drag options to blanks, or click blank then click option'
Ainsert
Bname
Cformat
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' instead of 'insert'
Using 'name' instead of 'format' for the third argument
Omitting the format array
5fill in blank
hard

Fill all three blanks to safely prepare and run a SQL query to get rows where name matches a variable.

Wordpress
$prepared_query = $wpdb->[1](
  "SELECT * FROM $table_name WHERE name = [2]",
  [3]
);
$results = $wpdb->get_results($prepared_query);
Drag options to blanks, or click blank then click option'
Aprepare
B'%s'
C$name
D$wpdb->prefix
Attempts:
3 left
💡 Hint
Common Mistakes
Not using prepare method
Using wrong placeholder like %d for strings
Passing wrong variable or prefix instead of $name