Last active
March 31, 2021 19:27
-
-
Save spivurno/4dfae7f6dd4bd15b8a6986436989c687 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Schedule a Post by Date Field (for Multiple Forms)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Schedule a Post by Date Field (for Multiple Forms) | |
* | |
* Schedule your Gravity Form generated posts to be published at a future date, specified by the user via GF Date and Time fields. | |
* | |
* @version 1.1 | |
* @author David Smith <david@gravitywiz.com> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/... | |
*/ | |
add_filter( 'gform_post_data', 'gw_schedule_post_by_date_field_multi_form', 10, 3 ); | |
function gw_schedule_post_by_date_field_multi_form( $post_data, $form, $entry ) { | |
$config = array(); | |
// Change '123' to your form ID; change '7' to your Date field ID; change '8' to your Time field ID. | |
$config['123'] = array( | |
'date' => 7, | |
'time' => 8 | |
); | |
// You can add as many forms as you want... | |
$config['433'] = array( | |
'date' => 7, | |
'time' => 8 | |
); | |
### don't touch the magic below this line ### | |
if( in_array( $form['id'], array_keys( $config ) ) ) { | |
$date = $config[ $form['id'] ]['date']; | |
$time = $config[ $form['id'] ]['time']; | |
} | |
$date = rgar( $entry, $date ); | |
$time = rgar( $entry, $time ); | |
if( empty( $date ) ) { | |
return $post_data; | |
} | |
if( $time ) { | |
list( $hour, $min, $am_pm ) = array_pad( preg_split( '/[: ]/', $time ), 3, false ); | |
if( strtolower( $am_pm ) == 'pm' ) { | |
$hour += 12; | |
} | |
} else { | |
$hour = $min = '00'; | |
} | |
$schedule_date = date( 'Y-m-d H:i:s', strtotime( sprintf( '%s %s:%s:00', $date, $hour, $min ) ) ); | |
$post_data['post_status'] = 'future'; | |
$post_data['post_date'] = $schedule_date; | |
$post_data['post_date_gmt'] = get_gmt_from_date( $schedule_date ); | |
$post_data['edit_date'] = true; | |
return $post_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👉 This Gist has been migrated to the Gravity Wiz Snippet Library:
https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-schedule-post-by-date-field-multi-form.php