Skip to content

Instantly share code, notes, and snippets.

@kasparsd
Created November 16, 2014 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasparsd/8580b4c109c118eb85a9 to your computer and use it in GitHub Desktop.
Save kasparsd/8580b4c109c118eb85a9 to your computer and use it in GitHub Desktop.
Capture form submissions from Contact Form 7 plugin for WordPress
<?php
add_action( 'wpcf7_before_send_mail', 'capture_entry' );
function capture_entry( $cf7 ) {
$posted_fields = array();
$submission = WPCF7_Submission::get_instance();
$posted_data_raw = $submission->get_posted_data();
// Extract field values. Be sure to take care of fields with multiple values!
foreach ( $posted_data_raw as $field_key => $field_value )
if ( ! preg_match( '/^(_wpnonce|_wpcf7)/i', $field_key ) )
$posted_fields[ $field_key ] = $field_value;
// Update post title and body content
wp_insert_post( array(
'post_type' => 'your-post-type',
'post_status' => 'draft',
'post_parent' => $cf7->id(),
'post_title' => $posted_fields['your-name'], // use any field or combination of fields
'post_content' => $posted_fields['your-message'] // use any field or combination of fields
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment