spivurno
9/20/2014 - 3:44 AM

Gravity Wiz // Gravity Forms // Schedule a Post by Date Field

Gravity Wiz // Gravity Forms // Schedule a Post by Date Field

<?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;
}
<?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'