---
title: Store Contact Form 7 Submissions in TablePress
date: 2020-07-10T10:39:10+00:00
modified: 2020-07-10T10:45:35+00:00
image:: https://kaspars.net/wp-content/uploads/2020/07/contact-form-7-tablepress.png
permalink: https://kaspars.net/blog/contact-form-7-tablepress
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
post_tag:
  - PHP
  - Snippet
category:
  - WordPress
---

# Store Contact Form 7 Submissions in TablePress

![Contact Form 7 to TablePress](https://kaspars.net/wp-content/uploads/2020/07/contact-form-7-tablepress.png?strip=all&quality=90&resize=1024,512)Here is a quick snippet of a PHP to store [Contact Form 7](https://wordpress.org/plugins/contact-form-7/) form submissions in [TablePress](https://wordpress.org/plugins/tablepress/) tables. Make sure you update the form-to-table-ID mapping in `$form_to_table_map` array to match your setup:

```
add_action(
	'wpcf7_before_send_mail',
	function ( $contact_form ) {
		// Map Contact Form ID to Table ID.
		$form_to_table_map = [
			4 => 1,
		];

		$form_id = $contact_form->id();

		if ( isset( $form_to_table_map[ $form_id ] ) && class_exists( 'TablePress' ) ) {
			$table_id = $form_to_table_map[ $form_id ];
			$table_model = TablePress::load_model( 'table' );
			$table = $table_model->load( $table_id );

			if ( is_array( $table['data'] ) ) {
				$form_data = WPCF7_Submission::get_instance()->get_posted_data();
				$table['data'][] = array_map( 'strval', $form_data );
				$table_model->save( $table );
			}
		}
	}
);
```

On each form submission it checks if the contact form ID matches an existing table ID and writes all field values to a new row in that table.

Be sure to register enough columns to fit all of contact form fields!