Description
This filter is executed right before the post is created, allowing post data to be manipulated before the post is created. Note: This filter only applies to forms that have Post Fields.
Usage
<?php
add_filter("gform_post_data", "change_post_data", 10, 3);
?>
Parameters
$post_data
- (Array)(required) The array containing the post data to be filtered. This array is in the format used by the WP function wp_insert_post($post_data)
$form
- (Form Object) The current form.
$entry
- (Entry Object) The current entry.
Examples
This example changes the default post type from post to page.
<?php
add_filter("gform_post_data", "change_post_type", 10, 3);
function change_post_type($post_data, $form, $entry){
//only change post type on form id 5
if($form["id"] != 5)
return $post_data;
$post_data["post_type"] = "page";
return $post_data;
}
?>
This example sets the post status to private.
<?php
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 5
if($form["id"] != 5)
return $post_data;
$post_data["post_status"] = "private";
return $post_data;
}
?>
This example sets the post date using date and time fields on the form.
<?php
add_filter("gform_post_data", "set_post_date", 10, 3);
function set_post_date($post_data, $form, $entry){
//only do this when the form id is 23
if($form["id"] != 23)
return $post_data;
//set post date to field on form if date provided; get from entry data
//using a date field in mm/dd/yyyy format and a time field in 24 hour format
$date_value = $entry["6"]; //pull the date value from the form data posted, field 6 on my form
$time = $entry["7"]; //pull the time value from the form data posted, field 7 on my form
//only change post date/time when a date has been chosen
if (!empty($date_value))
{
if (empty($time))
{
//set default time
$time_value = "00:00:00";
}
else
{
empty($time[0]) ? $time_hours = "00" : $time_hours = $time[0]; //pull hours out of array
empty($time[1]) ? $time_minutes = "00" : $time_minutes = $time[1]; //pull minutes out of array
$time_value = $time_hours . ":" . $time_minutes . ":00"; //add on seconds
}
//convert post date to the appropriate format so it will be inserted into the database
$post_date = date("Y-m-d H:i:s",strtotime($date_value . " " . $time_value));
$post_data["post_date"] = $post_date; //set the post_date
}
return $post_data; //return the changed data to use when the post is created
}
?>
Source Code
This filter is located in form_display.php