Hello fellow developers,
I’m working on a simple WordPress plugin designed to auto-submit a Gravity Forms form every 30 minutes using WP-Cron. However, the plugin doesn’t seem to be functioning as expected — the form is not being submitted automatically.
What the Plugin Should Do
Add a custom cron schedule to run every 30 minutes.
Use the Gravity Forms API to submit a specific form with predefined data.
Log any success or error messages for debugging purposes.
Issues Encountered
The scheduled task doesn’t appear to execute, and the form doesn’t submit as planned.
No errors are logged, even though I’ve enabled WP_DEBUG and checked the debug.log file.
Request
Could you please review the plugin code and help identify any potential issues? Here’s the complete plugin code:
<?php
/*
Plugin Name: Auto Submit Form Every 30 Minutes
Description: A simple plugin to auto-submit a specific Gravity Form every 30 minutes.
Version: 1.0
Author: Your Name
*/
// Prevent direct access to the file
if (!defined('ABSPATH')) {
exit;
}
// Add a new cron schedule for every 30 minutes
add_filter('cron_schedules', 'asf_add_half_hour_schedule');
function asf_add_half_hour_schedule($schedules) {
$schedules['half_hour'] = array(
'interval' => 1800, // 30 minutes in seconds
'display' => __('Every 30 Minutes')
);
return $schedules;
}
// Function to auto-submit the form
function asf_auto_submit_form() {
$form_id = 1; // ID of the form to submit
// Form data as an array, with field IDs as keys
$form_data = [
1 => 'User Name', // Example data for field ID 1
2 => '[email protected]', // Example data for field ID 2
3 => 'Sample description' // Example data for field ID 3
];
// Submit form using Gravity Forms API
$result = GFAPI::submit_form($form_id, $form_data);
if (is_wp_error($result)) {
error_log('Form submission error: ' . $result->get_error_message());
} else {
error_log('Form submitted successfully! Entry ID: ' . $result['entry_id']);
}
}
// Schedule the cron job if not already scheduled
function asf_schedule_auto_submit() {
if (!wp_next_scheduled('asf_auto_submit_form_event')) {
wp_schedule_event(time(), 'half_hour', 'asf_auto_submit_form_event');
}
}
add_action('wp', 'asf_schedule_auto_submit');
// Hook cron event to form submission function
add_action('asf_auto_submit_form_event', 'asf_auto_submit_form');
// Clear the scheduled task when the plugin is deactivated
function asf_clear_scheduled_auto_submit() {
wp_clear_scheduled_hook('asf_auto_submit_form_event');
}
register_deactivation_hook(__FILE__, 'asf_clear_scheduled_auto_submit');
Troubleshooting Steps I’ve Tried
Enabled WP_DEBUG and WP_DEBUG_LOG to check for errors.
Used the WP Crontrol plugin to verify if the cron event is scheduled, but it doesn’t seem to run.
Checked permissions and verified that the Gravity Forms plugin is active.
If you spot anything that could be causing this issue or have suggestions on how to debug further, I’d greatly appreciate it! Thanks in advance for your help!