Sebastien of Dev biznet
9/26/2019 - 4:15 PM

Contact Form 7 to Database (ACF)

Intercepte les données d'un Contact Form 7 pour les enregistrer en base de données. Dans le function.php.

<?php
  function wpcf7_submission_to_acf($wpcf7) {
  	$submission = WPCF7_Submission::get_instance();
  
  	if ($submission && $wpcf7->id == 3407) {		
  		$posted_data = $submission->get_posted_data();
  		
  		$row = array(
  		  'name' => sanitize_text_field($posted_data['name']),
  			'email' => sanitize_text_field($posted_data['mail']),
  			'telephone' => sanitize_text_field($posted_data['tel']),
  			'date' => date('d/m/Y G:i')
  		);
  		
  		add_row('inscriptions', $row, 3404);
  	}
  }
  add_action('wpcf7_mail_sent', 'wpcf7_submission_to_acf');
?>
<?php
function wpcf7_submission_to_acf($wpcf7) {
    $submission = WPCF7_Submission::get_instance();

    if ($submission && $wpcf7->id == 12654 ) {		
        $posted_data = $submission->get_posted_data();

        $field_1 = $_POST['name-1']; // Titre du post
        
        $my_post = array(
            'post_title' => $field_1, // // Titre du post
            'post_type' => 'rdv_event', // Nom du CPT où récupérer les données
          );
        $the_post_id = wp_insert_post( $my_post );
        
        update_post_meta( $the_post_id, 'nom', $_POST['name-1']); // 'nom' --> nom du champs ACF, ['name-1'] --> nom du champs dans le CF7
        update_post_meta( $the_post_id, 'prenom', $_POST['first-name-1']);
        update_post_meta( $the_post_id, 'telephone', $_POST['tel-1']);
        update_post_meta( $the_post_id, 'mail', $_POST['email-1']);
        update_post_meta( $the_post_id, 'message', $_POST['textarea-1']);
        update_post_meta( $the_post_id, 'date_de_rendez-vous', $_POST['date-1']);
        update_post_meta( $the_post_id, 'heure_de_rendez-vous', $_POST['heure-1']);
        update_post_meta( $the_post_id, 'date_de_mariage_mois', $_POST['mois-1']);
        update_post_meta( $the_post_id, 'date_de_mariage_annee', $_POST['annee-1']);
    }
}
add_action('wpcf7_mail_sent', 'wpcf7_submission_to_acf');

?>
if ($submission && ($wpcf7->id == 12654 OR $wpcf7->id == 12655)) {