Skip to content

Instantly share code, notes, and snippets.

@adczk
Last active February 20, 2023 08:43
Show Gist options
  • Select an option

  • Save adczk/c3674512fe001dbdede9f6bf2cb6394f to your computer and use it in GitHub Desktop.

Select an option

Save adczk/c3674512fe001dbdede9f6bf2cb6394f to your computer and use it in GitHub Desktop.
Forminator - schedule rendering form on page
<?php
/**
* Plugin Name: [Forminator] Schedule rendering form on page (form must be already published)
* Plugin URI: https://premium.wpmudev.org/
* Description: Allows rendering form according to schedule (show/hide)
* Author: adczk
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*
* Tested with Forminator 1.17.0 - 1.23.0
*
* config explained in code comment
*/
add_action( 'forminator_render_form_markup' , 'wpmu_form_render_before_time', 10, 4 );
function wpmu_form_render_before_time( $html, $form_fields, $form_type, $form_settings ) {
$hide_forms = array( 14150, 16091 ); // ids of IDs of forms to apply schedule to
// opening/closing dates and times
// left is openining, right is closing
// time zone according to current time zone of the SERVER
// use 24 hour format for time
// "parent" array key is form ID (must be same IDs as used in line above)
// "child" array key and value are, accordingly, start and end opening dates
// for this particular form
// see example below on how to specify them
$open_dates = array(
'14150' => array(
'2023-02-17 14:00' => '2023-02-17 15:00',
'2023-03-15 07:00' => '2023-03-16 07:00',
'2023-04-03 07:00' => '2023-04-05 9:00'
),
'16091' => array(
'2023-02-20 11:00' => '2023-02-20 15:00',
'2023-03-15 07:00' => '2023-03-16 07:00',
'2023-04-03 07:00' => '2023-04-05 9:00'
),
);
// message to show if form not yet available
// leave blank to not show any message and just remove the form
$msg = 'FORM NOT AVAILABLE CURRENTLY';
##### DO NOT EDIT BELOW #####
$the_form = $form_settings['form_id'];
if ( !in_array( $the_form, $hide_forms ) ) {
return $html; // if it's not scheduled form do nothing
}
$current_date = time();
$the_dates = $open_dates[$the_form];
foreach ( $the_dates as $do_open => $do_close ) {
if ( ( $current_date > strtotime( $do_open ) ) && ( $current_date < strtotime( $do_close ) ) ) {
$msg = $html; // if within any of defined date ranges, return original form
}
}
return $msg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment