WordPress Version: 4.5
/**
* Create database table, if it doesn't already exist.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $table_name Database table name.
* @param string $create_ddl Create database table SQL.
* @return bool False on error, true if already exists or success.
*/
function maybe_create_table($table_name, $create_ddl)
{
global $wpdb;
foreach ($wpdb->get_col('SHOW TABLES', 0) as $table) {
if ($table == $table_name) {
return true;
}
}
// Didn't find it, so try to create it.
$wpdb->query($create_ddl);
// We cannot directly tell that whether this succeeded!
foreach ($wpdb->get_col('SHOW TABLES', 0) as $table) {
if ($table == $table_name) {
return true;
}
}
return false;
}