Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Created December 9, 2024 07:11
Show Gist options
  • Save finalwebsites/a5f36bd0fc6af000b7c731d5e892c85d to your computer and use it in GitHub Desktop.
Save finalwebsites/a5f36bd0fc6af000b7c731d5e892c85d to your computer and use it in GitHub Desktop.
Contact Form 7 - Submit subscriber data to EmailOctopus
<?php
// Place this code into the functions.php file or your WordPress child theme
add_action('wpcf7_mail_sent', 'fw_wpcf7_subscribe_to_eo4wp' );
function fw_wpcf7_subscribe_to_eo4wp( $contact_form ){
// get form id
$form_id = $contact_form->id();
// get submission data
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();
if (!empty($posted_data['newsletter'])) {
$tag = 'newsletter';
$data = array('tags' => $tag, 'FirstName' => $posted_data['your-name']);
$list = get_option('fweo_emailoctopus_list_id');
$eo = new FWEO_EmailOctopus_integration();
$eo->add_subscriber($posted_data['your-email'], $list, $data);
}
}
@finalwebsites
Copy link
Author

With this code snippet it is possible to send the name and email address of your CF7 form submission to EmailOctopus.
The condition is that the visitor has ticked a checkbox with the name "newsletter".
To get it working, you can use the following CF7 form. Note the similarity of the field names that are used here and also in the PHP code.

<label> Your name
    [text* your-name autocomplete:name] </label>

<label> Your email
    [email* your-email autocomplete:email] </label>

<label> Your subject
    [text* your-subject] </label>

<label> Your message (optioneel)
    [textarea your-message] </label>

[checkbox newsletter use_label_element "Please send me your newsletter"]

[submit "Submit"]

The function or action hook works together with the plugin EmailOctopus for WordPress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment