How To Create Custom Database Tables for WordPress Plugin

In order to advance your WordPress plugin development (or even theme development), one of the steps you will need to make is learning how to create and work with additional database tables, which you can use to store information used by your plugin.

Why should you create tables in Your WordPress database?

By creating extra tables in the WordPress database, you will be able to create an infinite number of different kinds of plugins. A plugin-specific table allows you to define the exact data structure you need for your development.

An example of a plugin using a custom database table, is one of my latest plugins: Sugar Slider – Slide Manager. In Sugar Slider’s case, two separate DB tables are used to store sliders and the individual slides for each slider.

Creating tables is really pretty simple. All you need to do is pass an SQL query to a core WordPress database function.

Create Custom Tables For WordPress Plugins

This is all you need to create a table called “my_table_name” that has two columns, “one_column”, and “one_column”. If you need more information about the SQL syntax, refer to the W3 Schools page about it.

The code above, however, requires some sort of “init” function to make the database creation actually fire, so what we do is put it into a function that runs when our plugin is activated.

As long as this code is placed in your plugin’s main file, it will work great. When your plugin is activated, a new table will be created.

In upcoming posts, I will show you more about interacting with your plugin’s table and how to utilize these tools to create much better plugins.