Kevin.a
Kevin.a

Reputation: 4296

How to capture POST data with contact form7

I have this hook in my functions.php:

add_action( 'wpcf7_mail_sent', 'myfunction' );

I want to post the values when the form is sent.

I have a field like this: [textarea your-message].

How do i capture the POST data from this?

for example when the form is sent i want to echo the post value of [textarea your-message] in myfunction(){}

Upvotes: 7

Views: 33317

Answers (5)

Aqib
Aqib

Reputation: 354

You can retrieve form fields data through wpcf7_posted_data

add_filter( 'wpcf7_posted_data', function( $data ) {
  $name = $data['your-name'];
  // Do something with $name variable.
  return $data;
} );  

Upvotes: 2

kan
kan

Reputation: 11

Try This


add_action('wpcf7_before_send_mail','dynamic_addcc');
function dynamic_addcc($WPCF7_ContactForm){
    $currentformInstance  = WPCF7_ContactForm::get_current();
    $contactformsubmition = WPCF7_Submission::get_instance();
    if($contactformsubmition){
    $posted_data = $contactformsubmition->get_posted_data(); 
    }
}

Upvotes: 1

Shakil Hossain
Shakil Hossain

Reputation: 1743

This is how I used and its work for me to receive contact form 7 data after successe mail send and I used this data to send another server through API

add_action( 'wpcf7_mail_sent', 'your_wpcf7_mail_sent_function' ); 
function your_wpcf7_mail_sent_function( $contact_form ) {
    $title = $contact_form->title;
    $submission = WPCF7_Submission::get_instance();  
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }       
   if ( 'Reagistation' == $title ) {
        $name = strtolower($posted_data['text-name']);
        $name = strtolower(str_replace(' ', '_',  $name));
        $email = strtolower($posted_data['email']);
        $phone = strtolower($posted_data['phone']);
        $Areyouarealtor = $posted_data['Areyouarealtor'];
        $ayor = strtolower($Areyouarealtor['0']);

 }
}

Upvotes: 14

Rakhi Prajapati
Rakhi Prajapati

Reputation: 901

Try this :

add_action( 'wpcf7_sent', 'your_wpcf7_function' ); 

function your_wpcf7_function( $contact_form ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();

if ( $submission ) {
    $posted_data = $submission->get_posted_data();
}
   if ( 'MyContactForm' == $title ) {

    $firstName = $posted_data['first-name'];
    $lastName = $posted_data['last-name'];


   }
}

Upvotes: 7

yivi
yivi

Reputation: 47370

You need to access the $WPCF7_ContactForm object.

In your hooked function, you'd access the field you want like this:

yourFunction(&$WPCF7_ContactForm) {
    $text_area_contents = $WPCF7_ContactForm->posted_data['your-message'];
}

Upvotes: 3

Related Questions