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
add_filter( 'gform_post_data', 'your_function_name', 10, 3 );
Parameters
- $post_data array
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 form currently being processed.
-
$entry Entry Object
The entry currently being processed.
Examples
1. Change the post_type
This example changes the default post type from post to page.
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;
}
2. Change the post_status
This example sets the post status to private.
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;
}
3. Set the post date
This example sets the post date using date and time fields on the form.
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 = rgar( $entry, '6' ); //pull the date value from the form data posted, field 6 on my form
$time = rgar( $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 {
$time = explode( ':', $time ); //assumes you are using a time field in 24 hour format
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
}
4. Decode shortcodes in the post content
This example shows how you can decode the opening and closing brackets used by shortcodes in the post body field value.
add_filter( 'gform_post_data', function ( $post_data ) {
$find = array( '[', ']' );
$replace = array( '[', ']' );
$post_data['post_content'] = str_replace( $find, $replace, $post_data['post_content'] );
return $post_data;
} );
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GFFormsModel::create_post() in forms_model.php.