gform_after_create_post

gform_after_create_post

DescriptionUsageParametersExamples1. Update the post2. Convert date to the format expected by an ACF datepicker field type3. Update a post custom field with serialized GF checkboxesPlacementSource CodeSince

Description
This action hook is executed after the post has been created. It only applies to forms that have Post Fields.
Usage
The following would apply to all forms.
add_action( 'gform_after_create_post', 'your_function_name' );

To limit the scope of your function to a specific form, append the form id to the end of the hook name. (format: gform_after_create_post_FORMID)
add_action( 'gform_after_create_post_6', 'your_function_name' );

Parameters

$post_id integer
The ID of the post which was created from the form submission.

$entry Entry Object
The entry currently being processed. Available from 1.9.13.

$form Form Object
The form currently being processed. Available from 1.9.13.

Examples
1. Update the post
This example shows how you can update the post content, adding values from submitted fields, including an image field.
add_action( 'gform_after_create_post', 'set_post_content', 10, 3 );
function set_post_content( $post_id, $entry, $form ) {

//getting post
$post = get_post( $post_id );

//changing post content
$post->post_content = 'Blender Version:' . rgar( $entry, '7' ) . "

" . rgar( $entry, '13' ) . "
";

//updating post
wp_update_post( $post );
}

2. Convert date to the format expected by an ACF datepicker field type
The example below would take the date saved for a Gravity Forms field with id 30, convert it to the format expected by the ACF datepicker field type, and save it to the custom field meta key acf_date
Make sure to update the field id, the custom field meta key, and the form id number to make it work. Please read the snippet comments.
// Change 12 below to your form id number.
add_action( 'gform_after_create_post_12', 'gf_date_to_acf', 10, 3 );
function gf_date_to_acf( $post_id, $entry, $form ) {
GFCommon::log_debug( __METHOD__ . '(): running.' );
// Date as saved by GF. Change 30 to your date field id number.
$gf_date = rgar( $entry, '30' );
// Date changed to format expected by ACF.
$acf_date = date( 'Ymd', strtotime( $gf_date ) );
GFCommon::log_debug( __METHOD__ . '(): Date for ACF: ' . $acf_date );
// Save converted date to the acf_date meta key. Change it to your custom field meta key.
update_post_meta( $post_id, 'acf_date', $acf_date );
}

3. Update a post custom field with serialized GF checkboxes
This is useful for plugins like Advanced Custom Fields (ACF), Types and Pods where the values of the selections are stored in a serialized array. The scope of this example is limited to form id 1 and field id 18, you need to update these values to apply to your own form and field.
// Change 1 in gform_after_create_post_1 to your form id number.
add_filter( 'gform_after_create_post_1', 'gf_post_acf_checkboxes', 10, 3 );
function gf_post_acf_checkboxes( $post_id, $entry, $form ) {

// Checkboxes field id. Change this value to your field id number.
$field_id = 18;

// Get field object.
$field = GFAPI::get_field( $form, $field_id );

if ( $field->type == 'checkbox' ) {
// Get a comma separated list of checkboxes checked
$checked = $field->get_value_export( $entry );

// Convert to array.
$values = explode( ', ', $checked );
}

// Replace my_custom_field_key with your custom field meta key.
update_post_meta( $post_id, 'my_custom_field_key', $value );
}

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
gf_do_action( 'gform_after_create_post', $form['id'], $post_id, $lead, $form )
This hook is located in GFFormsModel::create_post() in forms_model.php.
Since
The form specific version of this hook was added in Gravity Forms 1.9.13.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注