What is a plug-in custom database table for a WordPress website?
Source: Internet
Author: User
When writing plug-ins, you may need to write something into the database. Generally, there are two types of data to be stored. The first type is to install the data, which is generally stored in the wp_options table, use the options mechanism of WordPress. The second type is data, which can be stored in the postmeta table or usermeta table. If there is a lot of data, you need to write something to the database when writing plug-ins separately. Generally, there are two types of data to be stored. The first type is to install data, it is generally stored in the wp_options table and implemented using the options mechanism of WordPress. The second type is data, which can be stored in the postmeta table or usermeta table. If there are a lot of data, you need to create a separate data table. This article describes how to create a data table when writing a plug-in.
Generally, there are three steps to create a database when writing a plug-in:
Create a PHP function to add a new data table.
Ensure that this function is called when the plug-in starts.
If the new version has different data table structures, create an upgrade function.
Today, I will take the new feature of the robot WordPress plug-in advanced edition: custom text reply. let's take a look at this process. The third step is not involved because no database upgrade is involved.
Create a PHP function for adding a new data table
The installation function of this plug-in is weixin_robot_texts_crate_table.
1. database table prefix
The default WordPress database tag prefix is wp _, but if someone installs two or more WordPress in a database, you need to set the WordPress database table prefix in the wp-config.php to distinguish, therefore, when adding a new table to the WordPress database, you must first find the database table prefix. We can find the variable $ wpdb-> prefix. As follows:
"Language-php"> global $ wpdb; $ table_name = $ wpdb-> prefix. "weixin_robot_texts ";
2. determine whether the table exists
It can be determined by a show tables SQL query.
"Language-php"> if ($ wpdb-> get_var ("show tables like '$ table_name '")! = $ Table_name)
3. create a data table
This step creates a data table. Our table has five fields: id, the ID of the custom text reply. Keywords sent by keyword, reply custom reply, time added, status enabled, sometimes you just don't want to enable it temporarily, not delete it.
Here I don't need to create a direct SQL query, but instead use dbDelta in wp-admin/upgrade-functions.php (we need to load the file, which is not loaded by default. The dbDelta function checks the current table structure and compares it with the expected table structure. Therefore, it is very convenient to add or modify a table. For details about how to use dbDelta, see the wp-admin/upgrade-schema.php examples.
"Language-php"> $ SQL = "CREATE TABLE". $ tablename ."(
'Id' bigint (20) not null AUTO_INCREMENT,
'Keyword'varchar (255) character set utf8 not null,
'Reply' text character set utf8 not null,
'Status' int (1) not null default '1 ',
'Time' datetime not null default '2017-00-00 00:00:00 ',
Primary key ('id '),
Unique key 'keyword' ('keyword ')
) ENGINE = MyISAM default charset = utf8;
";
Require_once (ABSPATH. 'WP-admin/shortdes/upgrade. php ');
Finally, the weixin_robot_texts_crate_table function is:
"11"> function weixin_robot_texts_crate_table (){
Global $ wpdb;
$ Table_name = $ wpdb-> prefix. "weixin_robot_texts ";
If ($ wpdb-> get_var ("show tables like '$ table_name '")! = $ Weixin_robot_texts_table)
{
$ SQL = "CREATE TABLE". $ table_name ."(
'Id' bigint (20) not null AUTO_INCREMENT,
'Keyword'varchar (255) character set utf8 not null,
'Reply' text character set utf8 not null,
'Status' int (1) not null default '1 ',
'Time' datetime not null default '2017-00-00 00:00:00 ',
Primary key ('id '),
Unique key 'keyword' ('keyword ')
) ENGINE = MyISAM default charset = utf8;
";
Require_once (ABSPATH. 'WP-admin/shortdes/upgrade. php ');
DbDelta ($ SQL );}}
Ensure that the plug-in calls the data table creation function when it starts.
We use register_activation_hook to call the function for creating a data table above.
"Language-php"> register_activation_hook (_ FILE __, 'weixin _ robot_texts_crate_table ');
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service