如何将WordPress中WPForms的表单数据存储至MySQL数据库?
Hey there! Let's break down your WPForms and MySQL questions clearly, step by step:
1. How to Store WPForms Data in a MySQL Database
You can do this by creating a custom database table (for clean, dedicated storage) and using WPForms' built-in hooks to capture and save submissions. Here's how:
Step 1: Create a Custom Database Table
First, add this code to create a table that will hold your form entries. It uses WordPress' database functions to handle prefixes and charset settings safely:
global $wpdb; $table_name = $wpdb->prefix . 'custom_wpforms_entries'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, form_id mediumint(9) NOT NULL, entry_data longtext NOT NULL, submitted_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql);
This creates a table with fields for entry ID, form ID, the full submission data (stored as JSON), and submission timestamp. If you want individual columns for each form field (e.g., name, email), modify the CREATE TABLE query to add those columns.
Step 2: Capture and Save Form Submissions
Use the wpforms_process_entry_save hook (triggered right after a form is submitted successfully) to insert data into your custom table:
add_action('wpforms_process_entry_save', 'save_wpforms_to_custom_mysql_table', 10, 3); function save_wpforms_to_custom_mysql_table($entry, $form_data, $entry_id) { global $wpdb; $table_name = $wpdb->prefix . 'custom_wpforms_entries'; // Convert entry data to JSON for easy storage/retrieval $entry_json = json_encode($entry); // Insert data into the table $wpdb->insert( $table_name, array( 'form_id' => $form_data['id'], 'entry_data' => $entry_json ), array('%d', '%s') // Data type formatting: integer, string ); }
If you opted for individual columns earlier, replace the entry_data line with code to extract specific fields. For example:
$name = $entry['fields'][1]['value']; // Replace 1 with your field's ID $email = $entry['fields'][2]['value'];
Then update the $wpdb->insert array to include these individual values.
2. Where to Add PHP Code for WPForms Data Storage
You have three safe, reliable options—avoid editing parent theme files directly (they'll get overwritten on updates):
- Code Snippets Plugin (Most Recommended): Install and activate the Code Snippets plugin, then create a new snippet, paste your PHP code, and click "Activate". This keeps your code separate from themes/plugins, so updates won't break it.
- Child Theme's
functions.php: If you're using a child theme, add your code to itsfunctions.phpfile. Child themes don't get overwritten when the parent theme updates. - Custom Plugin: Create a simple custom plugin by making a new PHP file (e.g.,
wpforms-mysql-storage.php) with this header, then add your code below it:
<?php /* Plugin Name: WPForms MySQL Storage Description: Stores WPForms submissions to a custom MySQL table Version: 1.0 Author: Your Name License: GPL2 */ // Paste your table creation and hook code here
Upload this file to wp-content/plugins/ and activate it from your WordPress dashboard.
3. Can the Free WPForms Version Store Data in a Database Table?
Absolutely! The free version doesn't have a one-click "Save to Database" feature like the Pro plan, but it fully supports custom code solutions using its built-in hooks (like the ones we used above).
Even without custom code, WPForms Free automatically stores submissions in WordPress' default database tables: entries are saved as a custom post type (wpforms_entry) in wp_posts, and field values are stored in wp_postmeta. But if you want a dedicated, clean table for your form data, the custom code approach works perfectly for the free version.
内容的提问来源于stack exchange,提问作者Sidath Rashen

