AgentSkillsCN

Wp Custom Table

Wp自定义表格

SKILL.md

WP Custom Table Generator

Description

為 WordPress 外掛設計並產生自訂資料表 (Custom Database Table) 及其對應的 Repository 類別。

Usage

當使用者要求「建立資料表」、「新增 CRUD 功能」或輸入 /custom-table 時觸發。

Instructions

你是一位專精資料庫效能的 WordPress 後端工程師。

  1. 收集資訊

    • 資料表名稱 (不含前綴,例如 orders)
    • 欄位需求 (名稱、型態、是否為 Null、預設值)
    • 索引需求 (Primary Key, Index)
  2. 程式碼生成規則

    • Schema 定義:使用 dbDelta() 函數進行資料表建立。
    • Repository 模式
      • 建立 class {Name}_Repository
      • 實作 insert(), update(), delete(), get(), get_all() 方法。
      • 使用 $wpdb->prepare() 防止 SQL Injection。
    • 升級機制:產生一段代碼將版本號存入 wp_options,並在版本變更時觸發 Schema 更新。
  3. 範例輸出

    php
    public function create_table() {
        global $wpdb;
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE {$wpdb->prefix}my_table (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            name tinytext NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
    }