Web Shakehands
Web Shakehands


How to create a database table from a WordPress plugin

I guess there are many occasions to create database tables for WordPress plugins.
This time, I will describe how to create a database table.

Below is the code.

First, write an option to record the version number of the database structure.

By using this code, this version information can be used when the table needs updating.

Example site

The $jal_db_version declared in the global variable is hooked by the last add_option ('jal_db_version', $ jal_db_version);

Next, declare $wpdb as a global variable and call the global object of the wpdb class.

Add a database table prefix.
"$wpdb->prefix" is the prefix.

Then conect the database table name with "." to the prefix.
This time it is "pvs_counter".

When it joints, it becomes like this.

Here, be careful not to use the same name in wp-config.php like wp_1. Each WordPress has a different name.

Getting the default character set.

Detailed information is written in Codex.

Specify the prefix and the table name like this CREATE TABLE $ table_name.

dbDelta ($ sql); is a function that checks if it is a WordPress rule when creating a table. You should pass this function when creating a table.
Also, dbDelta () is in wp-admin/includes/upgrade.php. It is not usable by default, so it needs to be included.

You have now created a table in the database.


This article title

How to create a database table from a WordPress plugin

Previous article Use current_time () when dealing with WordPress dates!

WordPress Plugin creation Article !

2019-07-25 Get information from the WordPress database using wpdb->prepare [plugin production] NEW!

2019-07-23 How to pass array to the database by using "update_option". WordPress Plugin Creation

2019-07-17 How to create a database table from a WordPress plugin

2019-07-13 Use current_time () when dealing with WordPress dates!

See all the articles list

Just enter your E-mail and you will receive free
mail magazine about how to make your website etc.

Go to top