主题初始化后创建数据表
wptutor
2023-11-22
52 Views
// Add a function to be called when WordPress initializes
add_action('init', 'create_custom_table_on_init');
function create_custom_table_on_init() {
global $wpdb;
// Define your table name
$table_name = $wpdb->prefix . 'custom_data_table';
// SQL statement to create the table if it doesn't exist
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT NOT NULL AUTO_INCREMENT,
fullname VARCHAR(100) NOT NULL,
cf_phone VARCHAR(20) NOT NULL,
cf_email VARCHAR(100),
cf_message TEXT,
user_verification_code VARCHAR(50) NOT NULL,
submission_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)";
// Execute the SQL query
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
}