Adding custom repeat schedules to the Event field
Adding custom repeat schedules to the Event field
Learn how to extend the Gravity Forms Event field with custom repeat frequency options using WordPress filters.
The Event field includes standard repeat frequency options (daily, weekly, monthly, yearly), but you can add custom schedules like "Every 3rd Monday" or "Last Friday of the month" using the developer filter system.
Prerequisites
Before you begin, ensure that you have:
- The Gravity Forms Event field plugin installed and activated
- Basic knowledge of WordPress hooks and filters
- Access to your site's
functions.phpfile or ability to create a custom plugin
Understanding the filter system
The Event field uses a filter-based system that allows developers to extend repeat frequency options. The system includes:
gk/event-field/repeat-options- Adds custom options to the repeat frequency dropdowngk/field-event/cron-value- Converts custom selections to cron expressions for scheduling
Step 1: Add custom repeat options
Add new options to the repeat frequency dropdown using the gk/event-field/repeat-options filter.
```php <?php /** * Add custom repeat frequency options to the Event field. * * This function extends the default repeat frequency dropdown with custom scheduling * options like "Every 3rd Monday" and "Last Friday of the month". The custom options * are merged with the existing default options (daily, weekly, monthly, yearly). * * @since 1.0.0 * * @param array $options The default repeat frequency options. * Contains key-value pairs where keys are * option values and values are display labels. * @param \GravityKit\EventField\EventField $field The current EventField instance. * Provides access to field properties and methods. * * @return array Modified options array with custom schedules added. * Maintains the original structure with additional custom options. */ function add_custom_event_repeat_options( $options, $field ) { // Add custom schedule options with translatable labels $custom_options = [ 'third_monday' => __( 'Every 3rd Monday', 'textdomain' ), 'last_friday' => __( 'Last Friday of the month', 'textdomain' ), 'first_tuesday' => __( 'First Tuesday of the month', 'textdomain' ), ];
// Merge custom options with existing default options
return array_merge( $options, $custom_options );
} add_filter( 'gk/event-field/repeat-options', 'add_custom_event_repeat_options', 10, 2 ); ```
Step 2: Convert custom options to cron expressions
Use the existing gk/field-event/cron-value filter to convert your custom repeat options into valid cron expressions.
```php <?php /** * Convert custom repeat frequency options to cron expressions. * * This function intercepts the cron array generation process and modifies it * for custom repeat frequency options. It reads the selected frequency from * the form submission and applies the appropriate cron pattern modifications. * * @since 1.0.0 * * @param array $cron { * The cron array containing scheduling components. * * @type string $minute Minute component (0-59, *, or pattern). * @type string $hour Hour component (0-23, *, or pattern). * @type string $day_month Day of month component (1-31, *, L, or pattern). * @type string $month Month component (1-12, *, or pattern). * @type string $day_week Day of week component (0-6, *, L, #, or pattern). * } * * @return array Modified cron array with custom schedule patterns applied. * Returns the original array if no custom schedule is detected. */ function handle_custom_event_cron_values( $cron ) { // Get the selected repeat frequency from the form submission $current_field_id = rgget( 'id' ); // Get the current form ID from query parameters $repeat_input_name = "input_{$current_field_id}_7"; // Field ID 7 is the repeat field $selected_frequency = rgpost( $repeat_input_name );
// Convert custom frequencies to cron expressions
switch ( $selected_frequency ) {
case 'third_monday':
// Every 3rd Monday: minute hour day_month month day_week
$cron['day_week'] = '1#3'; // Monday (1) on the 3rd occurrence (#3)
break;
case 'last_friday':
// Last Friday of the month
$cron['day_week'] = '5L'; // Friday (5) on the last occurrence (L)
break;
case 'first_tuesday':
// First Tuesday of the month
$cron['day_week'] = '2#1'; // Tuesday (2) on the 1st occurrence (#1)
break;
}
return $cron;
} add_filter( 'gk/field-event/cron-value', 'handle_custom_event_cron_values' ); ```
Cron expression reference
Understanding cron syntax helps you create precise scheduling patterns:
| Field | Values | Special Characters | |-------|--------|-------------------| | Minute | 0-59 | * , - / | | Hour | 0-23 | * , - / | | Day of month | 1-31 | * , - / ? L | | Month | 1-12 | * , - / | | Day of week | 0-6 (Sun=0) | * , - / ? L # |
Special characters for advanced scheduling
#- Nth occurrence (e.g.,1#3= 3rd Monday)L- Last occurrence (e.g.,5L= Last Friday)*/n- Every nth interval (e.g.,*/2= Every 2nd occurrence),- List separator (e.g.,1,3,5= Monday, Wednesday, Friday)-- Range (e.g.,1-5= Monday through Friday)
Best practices
Code organization
-
Create a custom plugin instead of adding code to
functions.phpfor better maintainability. -
Use proper text domains for translation-ready strings.
-
Add docblocks to all functions for documentation.
-
Validate user input to prevent invalid cron expressions.
Performance considerations
-
Cache complex calculations if your custom schedules require intensive processing.
-
Limit the number of custom options to prevent dropdown bloat.
-
Test cron expressions thoroughly before deploying to production.
User experience
-
Use clear, descriptive labels for custom repeat options.
-
Group related options logically in the dropdown.
-
Provide documentation for site administrators using custom schedules.
Troubleshooting
Common issues
Custom options don't appear in dropdown - Verify the filter is added correctly with proper priority - Check that your code is executed on form display pages - Ensure there are no PHP errors preventing filter execution
Cron expressions not working as expected - Validate cron syntax using online cron expression validators - Test with simple patterns before implementing complex schedules - Verify that your hosting environment supports the required cron features
Events not repeating correctly - Check that the cron expression is properly formatted - Ensure the calendar system (like GravityCalendar) supports your custom patterns - Verify timezone handling for complex schedules
Testing custom schedules
```php <?php /** * Debug function to test cron expression generation for custom schedules. * * This utility function helps developers test and validate their custom repeat * schedules by simulating form submissions and logging the generated cron expressions. * It's designed for development and debugging purposes only and includes proper * capability checks to prevent unauthorized usage. * * @since 1.0.0 * * @return void Outputs debug information to error log, no return value. */ function debug_custom_event_schedules() { // Security check: only allow administrators to run debug function if ( ! current_user_can( 'manage_options' ) ) { return; }
// Define test cases for various custom schedule types
$test_cycles = [ 'third_monday', 'last_friday', 'first_tuesday' ];
// Iterate through each test case to generate and log cron expressions
foreach ( $test_cycles as $cycle ) {
// Simulate form submission data for testing
$_POST["input_1_7"] = $cycle;
// Initialize default cron array structure
$cron = [
'minute' => '00',
'hour' => '09',
'day_month' => '*',
'month' => '*',
'day_week' => '*',
];
// Apply filters to generate custom cron expression
$result = apply_filters( 'gk/field-event/cron-value', $cron );
$expression = implode( ' ', $result );
// Log results for debugging and validation
error_log( "Custom Schedule Debug - Cycle: {$cycle} -> Generated Cron: {$expression}" );
}
// Clean up test data
unset( $_POST["input_1_7"] );
} // Uncomment the following line to enable debugging: // add_action( 'init', 'debug_custom_event_schedules' ); ```
Related articles
- Event field configuration guide
- GravityCalendar integration setup
- WordPress cron system overview
- Gravity Forms filter reference
> Note: Custom repeat schedules require the Event field plugin version 1.5.0 or higher. All code examples follow WordPress coding standards and include proper error handling.