Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spivurno/e8bf1e9fcb7991d8fa2f to your computer and use it in GitHub Desktop.
Save spivurno/e8bf1e9fcb7991d8fa2f to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Schedule a Post by Date Field
<?php
/**
* Schedule a Post by Date Field Usage
*/
// change the form ID
add_filter( 'gform_post_data_546', 'gw_schedule_post_by_date_field', 10, 3 );
// set the date and time values by field
$date = $entry['7']; // CHANGE: "7" to the ID of your Date field
$time = $entry['8']; // CHANGE: "8" to the ID of your Time field
// set date by field and time manually
$date = $entry['7']; // CHANGE: "7" to the ID of your Date field
$time = '09:00 am'
<?php
/**
* Gravity Wiz // Gravity Forms // Schedule a Post by Date Field
*
* 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.0
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
*/
// CHANGE: "546" to the ID of your form
add_filter( 'gform_post_data_546', 'gw_schedule_post_by_date_field', 10, 3 );
function gw_schedule_post_by_date_field( $post_data, $form, $entry ) {
$date = $entry['7']; // CHANGE: "7" to the ID of your Date field
$time = $entry['8']; // CHANGE: "8" to the ID of your Time field
### don't touch the magic below this line ###
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