Programmatically Creating Posts with Gravity Forms and Future Publish Date

I recently had a project where I built a site with WordPress that allowed users to submit a form via the Gravity Forms plugin which created a post on submission. It was a custom post type (which I had to use a separate plugin to handle) however out of the box, Gravity Forms can handle creating a regular old post.

The Issue

I found that when I used the code snippet that Gravity Forms posted in their documentation, the post status would never change to “scheduled” - it would immediately publish without updating the publish date.

Ultimately, I was trying to make the post publish after a 7 day waiting period. This allows the site admin time to review the post for it going live on the website. I could have just set the post status to “Draft” however the client wanted it to publish even if they didn’t get a chance to review it.

The Fix

So here is the final piece of code that inserted into the functions.php file. The fix was to set the post_date_gmt in addition to the post_date. Here is the code:


add_filter( 'gform_post_data', 'change_post_status', 10, 3 );

function change_post_status($post_data, $form, $entry){
    
    //only change post status on form id 1
    if ( $form['id'] != 1 ) {
       return $post_data;
    }
    
    // Get the current date
    $currdate = date('Y-m-d H:i:s');
    
    // Add 7 days to the current date
    $date = strtotime($currdate);
    $date = strtotime("+7 day", $date);
    $newpubdate = date('Y-m-d H:i:s', $date);
    
    
    //Set the post_date AND the post_date_gmt
    $post_data['post_date'] = $newpubdate; //set the post_date
    $post_data['post_date_gmt'] = $newpubdate;
    
    // Sets the post status to "Scheduled"
    $post_data['post_status'] = 'future';
    
    return $post_data;
    
}

Comments, Thoughts?

For now, please find me on Twitter (@erickarbe) and let me know what's on your mind. Or, find me on Google.