How To Get Contact Form 7 Data From Database

Obtaining data from the database of Contact Form 7 is a valuable skill to possess for those using the popular WordPress plugin. In this article, I will walk you through the steps of retrieving and modifying the form data saved in the database using PHP and MySQL.

First, let’s understand why you might need to access Contact Form 7 data from the database. The plugin stores form submissions in the WordPress database, allowing you to view and manage them within the WordPress dashboard. However, there may be cases where you need to access this data programmatically, such as exporting it to a CSV file or integrating it with another system.

To start, we need to connect to the WordPress database using PHP. We can achieve this by including the WordPress bootstrap file in our custom PHP script. Here’s an example:


require_once( 'wp-load.php' );

Once connected, we can use SQL queries to retrieve the form submission data from the database tables created by Contact Form 7. The main table we’ll be working with is typically named `wp_cf7dbplugin_submits`. We can query this table to retrieve the form data based on specific conditions.

Here’s an example query that retrieves all the form submissions:


global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM wp_cf7dbplugin_submits" );

This query retrieves all the rows from the `wp_cf7dbplugin_submits` table and stores them in the `$results` variable. You can then loop through the results to access individual form submissions and their respective data.

For example, you can display the form submission data on a webpage using a foreach loop like this:


foreach ($results as $result) {
echo 'Name: ' . $result->your-name . '
';
echo 'Email: ' . $result->your-email . '
';
// More fields...
}

You can access individual form fields by their respective column names in the `wp_cf7dbplugin_submits` table. In this example, we’re accessing the `your-name` and `your-email` fields.

If you need to filter the form submissions based on specific conditions, you can modify the SQL query accordingly. For example, you can retrieve only the form submissions from a specific contact form using the `form_name` column:


$results = $wpdb->get_results( "SELECT * FROM wp_cf7dbplugin_submits WHERE form_name = 'contact-form-1'" );

This query retrieves only the form submissions from a contact form with the name “contact-form-1”. You can replace “contact-form-1” with the actual name of your desired form.

Now that you have learned how to retrieve Contact Form 7 data from the database, you can customize and extend this functionality based on your specific needs. Remember to always sanitize and validate the data before using it to prevent any security vulnerabilities.

Conclusion

Accessing Contact Form 7 data from the database can be a valuable skill for WordPress developers. By connecting to the WordPress database and querying the appropriate tables, you can retrieve and manipulate form submission data programmatically. This allows you to perform various tasks such as exporting data or integrating it with other systems. Remember to always handle the data securely and follow best practices to ensure the integrity and privacy of the data.