gform_post_data

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 applies only to forms with Post Fields.

Usage

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

Parameters

ParameterTypeDescription
$post_dataarrayThe array containing the post data to be filtered. Most keys match the parameters accepted by the WP function wp_insert_post ($post_data)
$formForm ObjectThe form currently being processed.
$entryEntry ObjectThe entry currently being processed.

Most keys match the parameters accepted by the WP function wp_insert_post($post_data). This includes post_title, post_content, post_excerpt, post_status, post_category, and post_author. Custom field values, however, are passed as post_custom_fields, not meta_input as used by wp_insert_post(). Depending on the form’s fields, $post_data may also include tags_input (post tags) and images (an array of uploaded image data, each with field_id, url, title, description, caption, and alt).

Note: post_type is not included in $post_data by default; if you need to set it, add it using this filter (see Example 1 below), otherwise wp_insert_post() will default it to post.

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. 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 can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

This filter is located in GFFormsModel::create_post() in forms_model.php.