I am trying to submit a contact form 7 with the form id using WPCF7_ContactForm class that the plugin has
require_once(ABSPATH . 'wp-content/plugins/contact-form-7/includes/contact-form.php');
function submit_cf7_form_by_id($form_id, $form_data) {
// Create an instance of WPCF7_ContactForm class
$wpcf7_contact_form = WPCF7_ContactForm::get_instance($form_id);
// Check if the form instance exists
if ($wpcf7_contact_form) {
echo 'form found';
$submission = WPCF7_Submission::create($form_id);
// Populate form fields with data
foreach ($form_data as $field_name => $field_value) {
$submission->set_field($field_name, $field_value);
}
$result = $wpcf7_contact_form->submit($submission);
if ($result) {
return true;
} else {
return false;
}
} else {
return false;
}
}
I am running this function on wordpress init hook because I am currently testing
function cf7_submit_function() {
$form_id = '0aba882';
$form_data = array(
'your-name' => 'John Doe',
'your-email' => '[email protected]',
);
$submission_result = submit_cf7_form_by_id($form_id, $form_data);
if ($submission_result) {
echo "CF7 form submitted successfully.";
} else {
echo "Failed to submit CF7 form.";
}
}
add_action('init', 'cf7_submit_function');
But it is not getting the contact form 7 instance
I want to submit contact form without using default contact form submit functionality, so I can pass form data separately with form id and submit contact form through code