How to Create Tables in WordPress using BerlinDB
I recently discovered a wonderful module in WordPress which can help you create tables in WordPress easily.
Here is the Client Code. I did some modifications in the BerlinDB class as well so that I pass the table columns in the Client code.
$columns = [
//id
'id' => [
'name' => 'id',
'type' => 'bigint',
'length' => '20',
'unsigned' => true,
'extra' => 'auto_increment',
'primary' => true,
'unique' => true,
'sortable' => true,
],
//url
'url' => [
'name' => 'url',
'type' => 'mediumtext',
//'length' => '127',
//'unique' => true,
'unsigned' => true,
'searchable' => true,
'sortable' => true,
],
//title
'title' => [
'name' => 'title',
'type' => 'mediumtext',
'unsigned' => true,
'searchable' => true,
'sortable' => true,
],
//author
'author' => [
'name' => 'author',
'type' => 'mediumtext',
'unsigned' => true,
'searchable' => true,
'sortable' => true,
],
//date_created
'date_created' => [
'name' => 'date_created',
'type' => 'datetime',
'date_query' => true,
'unsigned' => true,
'searchable' => true,
'sortable' => true,
],
//date_published
'date_published' => [
'name' => 'date_published',
'type' => 'datetime',
'date_query' => true,
'unsigned' => true,
'searchable' => true,
'sortable' => true,
],
Here I’m setting the columns for the table. Now I need to pass these value to the table Class, which will create a table in the DB.
/**
* TABLE SETUP
* This creates the database tables, and add the table to the database.
*/
// Instantiate the Books Table class.
$table = new Build_Table( 'my_links', $columns);
The above code will create a table named wp_my_links with columns
Here you can find the complete code in GitHub.