gform_advancedpostcreation_post_after_creation

Description

Action hook which allows further actions to be performed after the post has been created.

Usage

The following would apply to all forms:

add_action( 'gform_advancedpostcreation_post_after_creation', 'your_function_name', 10, 4 );

To target a specific form, append the form id to the hook name. (format: gform_advancedpostcreation_post_after_creation_FORMID):

add_action( 'gform_advancedpostcreation_post_after_creation_1', 'your_function_name', 10, 4 );

Parameters

Examples

Send notification after post creation

add_action( 'gform_advancedpostcreation_post_after_creation', 'after_post_creation', 10, 4 );
function after_post_creation( $post_id, $feed, $entry, $form ){
    GFCommon::send_email( 'test@example.com', 'test@example.com','','','New Post', 'A new post was created.');
}

Update a post custom field with serialized GF checkboxes or Multi Select

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. Passing an array to update_post_meta will automatically serialize the array.

Once the snippet is added to your site, simply put choices_array in the Custom CSS Class setting for the field(s) where you want the snippet to run, and the meta key name for the custom field in the field Admin Field Label setting.

// Save multiple choices fields (Checkbox or Multiselect) as serialized array to custom post meta.
add_filter( 'gform_advancedpostcreation_post_after_creation', function( $post_id, $feed, $entry, $form ) {
	gf_advancedpostcreation()->log_debug( 'Running choices to array function...' );

	foreach ( $form['fields'] as &$field ) {
		gf_advancedpostcreation()->log_debug( "(): Field id: {$field->id}, Type: {$field->type}, CSS: {$field->cssClass}, Admin Label: {$field->adminLabel}" );
		if ( strpos( $field->cssClass, 'choices_array' ) !== false ) {
			// Get a comma separated list of choices selected.
			$selected_choices = $field->get_value_export( $entry );
			gf_advancedpostcreation()->log_debug( "Values: {$selected_choices}" );
			if ( ! empty( $selected_choices ) && ! empty( $field->adminLabel ) ) {
				// Convert to array. WordPress will automatically serialize this array when saved to the post meta.
				$values = explode( ', ', $selected_choices );
				gf_advancedpostcreation()->log_debug( 'Array: ' . print_r( $values, true ) );

				// Save the value to the post meta using the Field Admin Label as the meta key name.
				update_post_meta( $post_id, $field->adminLabel, $values );
			}
		}
	}

}, 10, 4 );

This code snippet can be used to add images added through File/Upload fields and mapped to the Product Post type in the Media Library. This will take any images attached to the post for the product and add them to the product gallery in WooCommerce.

// Add images to WooCommerce product gallery. Change 1909 on the following line to your form id number.
add_action( 'gform_advancedpostcreation_post_after_creation_1909', 'apc_add_images_to_product_gallery', 10, 4 );
function apc_add_images_to_product_gallery( $post_id, $feed, $entry, $form ) {
    GFCommon::log_debug( __METHOD__ . '(): running.' );
 
    $attached_images = get_attached_media( 'image', $post_id );
    GFCommon::log_debug( __METHOD__ . '(): Images attached to Post: ' . print_r( $attached_images, true ) );
    $product_gallery = implode( ',', array_keys( $attached_images ) );
    GFCommon::log_debug( __METHOD__ . '(): Product gallery ID\'s: ' . $product_gallery );
 
    // Add the meta key with the comma separated ID's for images attached to the post.
    update_post_meta( $post_id, '_product_image_gallery', $product_gallery );
}

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?

Since

This filter was added in Gravity Forms Advanced Post Creation add-on version 1.0.

Source Code

This filter is located in GF_Advanced_Post_Creation::create_post() in class-gf-advancedpostcreation.php.