Hongmu Notes
Home Program Notes WordPress plugin development: Creating corresponding data tables
Program Notes wordpress

WordPress plugin development: Creating corresponding data tables

WordPress plugin development: Creating corresponding data tables

Here is a simple WordPress plugin example that will create the corresponding database table when enabled:

  1. Create a file named my-database-plugin Create a folder and within it, create a file named my-database-plugin.php Document.

  2. my-database-plugin.php Add the following code to the document:

    <?php
    /**
     * Plugin Name: 红穆插件
     * Plugin Script: hongmu-zibi.php
     * Plugin URI: http://4s5.cn
     * Description: 开发插件的示范
     * Version: 1.3.0
     * License: GPL
     * Author: 红穆
     * Author URI: http://4s5.cn/
     * Text Domain: hongmu-zibi
     */
    
    // 如果直接访问该文件则退出。
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    /**
     * 定义数据库表名。
     */
    global $wpdb;
    define( 'MY_DATABASE_TABLE', $wpdb->prefix . 'my_database_table' );
    
    /**
     * 在插件激活时创建数据库表。
     */
    function my_database_plugin_activate() {
    	global $wpdb;
    	$table_name = MY_DATABASE_TABLE;
    	$charset_collate = $wpdb->get_charset_collate();
    
    	$sql = "CREATE TABLE $table_name (
    		id int(11) NOT NULL AUTO_INCREMENT,
    		name varchar(255) NOT NULL,
    		email varchar(255) NOT NULL,
    		PRIMARY KEY  (id)
    	) $charset_collate;";
    
    	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    	dbDelta( $sql );
    }
    register_activation_hook( __FILE__, 'my_database_plugin_activate' );
    
    /**
     * 在插件停用时删除数据库表。
     */
    function my_database_plugin_deactivate() {
    	global $wpdb;
    	$table_name = MY_DATABASE_TABLE;
    
    	$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
    }
    register_deactivation_hook( __FILE__, 'my_database_plugin_deactivate' );
    
    /**
     * 添加插件菜单页面。
     */
    function my_database_plugin_menu() {
    	add_menu_page(
    		'我的数据库插件',
    		'我的数据库插件',
    		'manage_options',
    		'my-database-plugin',
    		'my_database_plugin_page',
    		'dashicons-database',
    		30
    	);
    }
    add_action( 'admin_menu', 'my_database_plugin_menu' );
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

What should I do if the Redis Object Cache plug-in cannot be linked after wordpress redis changes the password?

What should I do if the Redis Object Cache plug-in cannot be linked after wordpress redis changes the password? Program Notes wordpress

Problem description: My wordpress site server uses pagoda panel, installed php8.0, and installed redis on the extension panel, and installed Redis Object Cache plug-in in wordpress. In fact, in general, you don't need to do anything, just start it directly. However, it is unsafe for radis not to set a password, which is a risky loophole, so I still think it is better to set a password, …
👁 255
How do I disable revisions and automatic draft saving in WordPress?

How do I disable revisions and automatic draft saving in WordPress? Program Notes wordpress

WordPress's automatic article revision tracking feature logs every time you edit an article in the后台; each revision is recorded as a separate entry in the `wp_posts` table. Due to the interplay between article revisions and automatic saving, the article ID often grows larger over time. While this typically does not cause significant issues for your WordPress installation, an excessive number of article versions can place a considerable burden on your storage space and database...
👁 372
Detailed Tutorial on Migrating EmpireCMS Blog Data to WordPress

Detailed Tutorial on Migrating EmpireCMS Blog Data to WordPress Program Notes wordpress

PS1: Second revision – now supports data transfer between categories, posts, and tags. PS2: If the post IDs consistently don't match, you can try clearing the WordPress post category data table and relationship tables. I previously used Typecho for my blog, but later migrated to Empire; however, Empire has too many features – using it for a blog feels like putting a large hammer into a small hole! Additionally, I often record important code snippets in my blog – all of which I've painstakingly written...
👁 546
WordPress issue: pages freezing due to image transcoding

WordPress issue: pages freezing due to image transcoding Program Notes wordpress

Last night, while uploading an image, for some reason, the uploaded image was automatically transcoded. Transcoding type: BASE64 encoding. For an image on a website, simply upload it to your server and then access the link – the image will then be displayed. However, there is another approach: directly transcoding the image; then you can simply access the converted image using its encoded URL. Problem description: When an image is relatively small (e.g., only a few KB in size), transcoding it and then hosting it on a website can help avoid...
👁 152
WordPress database query operations

WordPress database query operations Program Notes wordpress

To connect to a database for a WordPress installation, you need to include the `wp-config.php` file in your PHP script. This file contains the database configuration details for WordPress installation and other constants. Here is a simple example demonstrating how to include the `wp-config.php` file and establish a database connection: // Include the wp-config.php file: require...
👁 377
WordPress MySQL Database Table and Subtable Structure Functionality Guide

WordPress MySQL Database Table and Subtable Structure Functionality Guide Program Notes wordpress

WordPress uses a MySQL database. Its data table structure differs slightly from that of conventional databases; data can be stored either in the form of standard data tables or as data elements – the latter approach is less intuitive when viewed directly. To become a developer, you must understand the basic structure of the WordPress database and be able to use it within your own plugins or themes to write to or read from the database. I. Data Tables – As of WordPress...
👁 374

Recommended reading

Responsive English Website Template for Perfume Export Enterprises – 0479

Responsive English Website Template for Perfume Export Enterprises – 0479 Practical Collection Yiyou template

An EyouCMS responsive English-language website template designed for international perfume trading enterprises. Its contemporary, international design style effectively showcases perfume products, brand stories, export advantages, and global market presence. The English version enables perfume brands to present their brand image effectively in the international market. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 48
(Adaptive mobile version) PBootCMS template for landscape architecture websites; Download source code for landscape architecture design websites – 0783

(Adaptive mobile version) PBootCMS template for landscape architecture websites; Download source code for landscape architecture design websites – 0783 Practical Collection pbootcms Template

A PbootCMS website template for landscape architecture and garden design, compatible with both PC and WAP devices. Featuring a natural and artistic design style, it is ideal for showcasing landscape design projects, garden engineering works, and plant arrangements. This template helps landscape design firms present their projects online, attracting property owners and developers. Template Demo | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 43
Apple cms template development manual

Apple cms template development manual Program Notes Apple cms

If you want to write a theme when developing a template, first create a theme_tpl directory in the template directory as our customized template directory, and then create an html folder to store all html files └─ template └─ `theme_tpl` ├─ css ├─ js └─ For html details, please refer to Template Structure to build your template structure...
👁 214
Home Furnishings & Interior Design – International Trade Responsive English Website Template 0480

Home Furnishings & Interior Design – International Trade Responsive English Website Template 0480 Practical Collection Yiyou template

This EyouCMS responsive English-language website template is ideal for the home decoration and furniture export industries. Its international, professional design style effectively showcases furniture products, home decoration solutions, export advantages, and global market presence. It helps home furnishing brands enhance their brand image in the international market and secure overseas orders. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 30
(Adaptive mobile version) PBootCMS template for spray painting and powder coating equipment websites; Download source code for blow molding machinery and equipment websites – 0784

(Adaptive mobile version) PBootCMS template for spray painting and powder coating equipment websites; Download source code for blow molding machinery and equipment websites – 0784 Practical Collection pbootcms Template

A PbootCMS website template for spray coating and blow molding machinery, compatible with both PC and WAP devices. The professional and practical design is ideal for showcasing spray coating equipment, blow molding machines, and other products along with their technical specifications. It helps machinery manufacturers showcase their products online and attract industrial manufacturing clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 50
Apple cmsV10 imitation site actual combat personal vlog video

Apple cmsV10 imitation site actual combat personal vlog video Program Notes Apple cms

Contact information 🔒 Hidden content: Check after commenting. I am Hongmu, QQ1810216796 communication group: 958972323 If you have any questions, please join the group to communicate. Preparation of website imitation tools in the early stage: https://wwi.lanzoup.com/iHqCOs7m06d Configuration path: /template/skin/static/PHPstudy: Windows version of phpstu…
👁 413