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
    // }

Additionally i tried with cURL but couldn't get it working

function submit_cf7_form_data()
    $form_id = 452; Contact Form 7 form ID
    $apiUrl = 'https://example.com/wp-json/contact-form-7/v1/contact-forms/' . $form_id . '/feedback';
    $userAgent = 'WordPress/' . get_bloginfo('version') . '; ' . get_bloginfo('url');
    $header = ['Content-Type: multipart/form-data'];
    $body = [
        'your-name' => 'form daily check',
        'company' => 'form daily check',
        'your-email' => '[email protected]',
        'your-subject' => 'form daily check',
        'your-message' => 'form daily check',

    $curlOpts = [
        CURLOPT_POST => 1,
        CURLOPT_USERAGENT => $userAgent,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => $body,

    $ch = curl_init($apiUrl);
    curl_setopt_array($ch, $curlOpts);
    $response = curl_exec($ch);

    if ($response === false) {
        // Handle cURL error
        $error_msg = curl_error($ch);
        // Log or display the error message
        echo "cURL Error: " . $error_msg;
    } else {
        // Decode the JSON response
        $decoded_response = json_decode($response);
        if ($decoded_response === null) {
            // Handle JSON decoding error
            // Log or display the error
            echo "Error decoding JSON response";
        } else {
            // Handle successful response
            // Output the decoded response

    // Close cURL resource

// Schedule the function to run with WP Cron
add_action('init', 'schedule_cf7_form_submission');

function schedule_cf7_form_submission()
    if (!wp_next_scheduled('submit_cf7_form_data_event')) {
        wp_schedule_event(time(), 'every-2-minutes', 'submit_cf7_form_data_event');

// Hook the function to the WP Cron event
add_action('submit_cf7_form_data_event', 'submit_cf7_form_data');

And even with ajax but without success. In this scenario i pasted the js code directly into the page with a page builder and with ajax i'm trying to call the function :

// AJAX action
add_action('wp_ajax_nopriv_daily_form_check', 'daily_form_check_callback');
add_action('wp_ajax_daily_form_check', 'daily_form_check_callback');

// WP-Cron event
add_action('init', 'schedule_daily_form_check_event');

function schedule_daily_form_check_event()
    if (!wp_next_scheduled('daily_form_check_event')) {
        wp_schedule_event(time(), 'every-2-minutes', 'daily_form_check_event');

// Debugging: Check if the event is scheduled
function check_scheduled_event()
    if (wp_get_scheduled_event('daily_form_check_event')) {
        echo 'Scheduled event is active'; // For testing purposes
    } else {
        echo 'Scheduled event is not active'; // For testing purposes
    exit; // Exit

// add_action('wp_loaded', 'check_scheduled_event');

function daily_form_check_callback()
    // Check if the scheduled event is active
    if (wp_get_scheduled_event('daily_form_check_event')) {
        // Check if the current page matches the desired page ID
        if (is_page(2)) {
            // call function
            echo "<script>dailyFormCheckScript();</script>";
        } else {
            // Page does not match the desired page ID, return an error message
            echo 'AJAX call restricted to a specific page';
    } else {
        // Scheduled event is not active, return an error message
        echo 'Scheduled event is not active';

    // exit

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

  • stackoverflow.com/questions/59878327/… Can you check here.
    – Aftab
    Feb 12 at 4:13
  • I saw that. I'm just interested what am i doing wrong in my case. Why the event is scheduled and runs but it doesn't submit the form with javascript. Maybe i should use ajax ?
    – Ion T
    Feb 12 at 11:34
  • @Aftab I actually tried the cURL method and i also couldn't get it working. No error, no sign ...
    – Ion T
    Feb 12 at 23:05


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.