Web Shakehands
Web Shakehands

Language


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
https://codex.wordpress.org/Creating_Tables_with_Plugins#A_Version_Option

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.
http://wpdocs.osdn.jp/%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%A7%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E3%82%92%E4%BD%9C%E3%82%8B

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.
E-mail:
  

Go to top