The gform_post_data
filter in Gravity Forms allows you to manipulate the post data right before the post is created.
Usage
To use this filter, you can add it to your theme’s functions file (functions.php) or a site-specific plugin. Here’s a simple example:
add_filter( 'gform_post_data', 'modify_post_data', 10, 3 ); function modify_post_data( $post_data, $form, $entry ) { // your custom code here return $post_data; }
In the code above, modify_post_data
is your custom function that will be triggered before the post is created.
Parameters
- $post_data (array): This is the array containing the post data to be filtered. The array is in the format used by the WordPress function wp_insert_post.
- $form (array): Current form array.
- $entry (array): Current entry array.
More information
See Gravity Forms Docs: gform_post_data
This filter is only applied to forms that have Post Fields.
Examples
Change Post Title
add_filter( 'gform_post_data', 'change_post_title', 10, 3 ); function change_post_title( $post_data, $form, $entry ) { // Changing the post title $post_data['post_title'] = 'New Title'; return $post_data; }
In this example, the post title is being changed to ‘New Title’ before the post is created.
Append Text to Post Content
add_filter( 'gform_post_data', 'append_post_content', 10, 3 ); function append_post_content( $post_data, $form, $entry ) { // Appending text to the post content $post_data['post_content'] .= ' Additional Content'; return $post_data; }
This example adds ‘Additional Content’ to the end of the post content.
Change Post Status to Draft
add_filter( 'gform_post_data', 'change_post_status', 10, 3 ); function change_post_status( $post_data, $form, $entry ) { // Changing the post status to 'draft' $post_data['post_status'] = 'draft'; return $post_data; }
This example changes the post status to ‘draft’ before the post is created.
Set Post Author
add_filter( 'gform_post_data', 'set_post_author', 10, 3 ); function set_post_author( $post_data, $form, $entry ) { // Setting the post author to user with ID 2 $post_data['post_author'] = 2; return $post_data; }
This example sets the post author to the user with ID 2.
Add Custom Post Meta
add_filter( 'gform_post_data', 'add_custom_post_meta', 10, 3 ); function add_custom_post_meta( $post_data, $form, $entry ) { // Adding custom post meta add_post_meta( $post_data['ID'], 'custom_meta_key', 'custom_meta_value' ); return $post_data; }
This last example adds custom post meta to the post before it is created. Please note, you might need to adjust this example based on your specific use case, as post ID might not be available at this stage.