functions – Submit CF7 form programmatically with WP-Cron


We need to check daily if our CF7 form is working properly (form submission data arrives to support’s email). So i need a way to programmatically submit the form on frontend.

I tried with WP-Cron but my solution doesn’t do anything even if i force-run it with WP Crontrol plugin.

The JS code alone works. It populates all form fields and submits the form.

  • event must run daily
  • on event run it must execute the js code to submit the form on frontend and preferably on a page where form is actually used (not sure if is_page is enough for this task)

Below is my code in functions.php:

// Daily contact form check
add_action('init', 'cf7_email_daily_cron_schedule');

function cf7_email_daily_cron_schedule()
    if (!wp_next_scheduled('cf7_contact_form_check')) {
        wp_schedule_event(time(), 'daily', 'cf7_contact_form_check');
// Execute JavaScript function when WP Cron event is triggered
add_action('cf7_contact_form_check', 'cf7_email_daily_cron_js_script');
function cf7_email_daily_cron_js_script()

    // if (is_page(1370)) {
        document.addEventListener('DOMContentLoaded', function() {

            var wpcf7Form = document.querySelector('.wpcf7-form');

            if (wpcf7Form) {
                wpcf7Form.querySelector('input[name="your-name"]').value="form daily check";
                wpcf7Form.querySelector('input[name="company"]').value="form daily check";
                wpcf7Form.querySelector('input[name="your-email"]').value="[email protected]";
                wpcf7Form.querySelector('select[name="your-subject"]').value="form daily check";

                var subjectDropdown = wpcf7Form.querySelector('select[name="your-subject"]');
                subjectDropdown.innerHTML = '';

                // Create and append a new option
                var customOption = document.createElement('option');
                customOption.value="form daily check";
                customOption.text="form daily check";

                // Select the newly added option
                customOption.selected = true;

                wpcf7Form.querySelector('textarea[name="your-message"]').value="form daily check";

                // Submit the form
    // }

Please help, maybe somebody faced a similar issue. I am aware that wp-cron can have delays and isn’t that reliable.

Ion T 2 weeks 2024-02-11T18:50:04-05:00 0 Answers 0 views 0

Leave an answer