Advanced Post Creation Add-On Using Third-Party Post Types

Introduction

One of the features of the Advanced Post Creation Add-On allows posts to be created upon form submission for third-party post types. This articles provides a guide on how to use Advanced Post Creation with the plugins WooCommerce, to create Products, and The Events Calendar, to create Events.

Note: This article assumes you have installed and activated the required plugins. If not, you will need to do so before any of the functionality in this article can be used.

Creating Products for WooCommerce

Create the Form

Several pieces of information need to be captured to create a Product. Below captures some basic information.

Field TypeLabelOther Settings
Drop DownCategorySee Populating Drop Down with Product Categories below for details.
Single Line TextProduct Name 
ParagraphDescription 
NumberPriceSet the number format to currency
File UploadProduct Image 

Create the Advanced Post Creation Feed

For more details about how to create the feed, review the Creating A Feed For The Advanced Post Creation Add-On article.

  1. Expand the drop down for Type in the Post Settings section and select Product.
  1. Scroll to the Post Content section and map the Title to the Product Name field.
  2. Map the Content to the Description field.
  3. Map the Featured Image to a File Upload field labeled Image. If the File Upload field is not present on your form this field will not appear in Post Creation settings.
  1. Click Save Settings.

Note: The Price and Category will be handled separately because they cannot be directly mapped.

Handling Fields Unable to be Mapped

Not all fields needed for the product can directly be mapped in the feed. The gform_advancedpostcreation_post_after_creation action hook will be used to update the Price and Category for the product. Below are examples for two of the Product fields which need to be handled specially.

For more details about the hook parameters, placement, and usage, see the documentation for the gform_advancedpostcreation_post_after_creation hook mentioned above.

Price

The price information is stored in the wp_post_meta table and the update_post_meta() function will be used to save this information.

Category

The product category utilizes multiple WordPress taxonomy and term database tables and the wp_set_object_terms() function will be used to save this information.

The example below updates the Price using the value stored in field 7 of the entry. The Category is updated using the value stored in field 6 of the entry.

add_action( 'gform_advancedpostcreation_post_after_creation', 'update_product_information', 10, 4 );
function update_product_information( $post_id, $feed, $entry, $form ){

	//update the prices set for the product
	update_post_meta( $post_id, '_regular_price', $entry['7'] );
	update_post_meta( $post_id, '_price', $entry['7'] );

	//update the category
	wp_set_object_terms( $post_id, $entry['6'], 'product_cat' );

}

Populating Drop Down with Product Categories

Note: This example expects the product categories to not have children. If there are child categories, the example will need to be modified as needed.

Several filters are needed to populate the drop down. The article Dynamically Populating Drop Down Or Radio Buttons Fields has more information. The filters used are:

add_filter( 'gform_pre_render_83', 'populate_dropdown_with_product_categories' );
add_filter( 'gform_pre_submission_filter_83', 'populate_dropdown_with_product_categories' );
add_filter( 'gform_admin_pre_render_83', 'populate_dropdown_with_product_categories' );
add_filter( 'gform_pre_validation_83', 'populate_dropdown_with_product_categories' );
function populate_dropdown_with_product_categories( $form ) {
	//product_cat is the taxonomy for WooCommerce's products
	//get the terms for the product_cat taxonomy
	$product_categories = get_terms( 'product_cat' );

	//Creating drop down item array.
	$items = array();

	//Adding initial blank value.
	$items[] = array( 'text' => '', 'value' => '' );

	//Adding product category terms to the items array
	foreach ( $product_categories as $product_category ) {
		$items[] = array( 'value' => $product_category->name, 'text' => $product_category->name );
	}

	//Adding items to field id 6. Replace 6 with your actual field id. You can get the field id by looking at the input name in the markup.
	foreach ( $form['fields'] as &$field ) {
		if ( $field->id == 6 ) {
			$field->choices = $items;
		}
	}

	return $form;
}

Creating Events for The Events Calendar

Create the Form

Several pieces of information need to be captured to create an Event. Below captures some basic information.

Create a new form and add the following fields.

TypeLabelOther Settings
Drop DownCategorySee Populating Drop Down with Event Categories below for details.
Single Line TextEvent Title 
ParagraphEvent Description 
CheckboxesEvent OptionsChoices: All Day, Hide From Event Listings, Sticky in Month View, Feature Event
DateStart Date 
TimeStart TimeConditional Logic: Show field if Event Options is not All Day
DateEnd Date 
TimeEnd TimeConditional Logic: Show field if Event Options is not All Day

Create the Advanced Post Creation Feed

For more details about how to create the feed, review the Creating A Feed For The Advanced Post Creation Add-On article.

  1. Expand the drop down for Type in the Post Settings section and select Event.
  1. Scroll to the Post Content section and map the Title to the Event Title field.
  2. Map the Content to the Event Description field.
  3. In the Custom Fields section, in the dropdown “Select a Custom Field Name” scroll to the bottom of the list and enter this custom field name: _EventStartDate. Map that to the Start Date field in your form. Repeat for the custom field name _EventEndDate and map that custom field to the End Date field in your form.

    Mapping custom fields for the event start date and event end date
  4. Scroll to the Taxonomies section and map the Event Categories to Field Value and Category field.
Mapping taxonomy field for event category
  1. Click Save Settings.

Handling Fields Unable to be Mapped

Not all fields needed for the event can directly be mapped in the feed. The gform_advancedpostcreation_post_after_creation action hook will be used to update these fields. Below are examples of several fields which need to be handled specially.

See the article mentioned above for more details about the hook parameters, placement, and usage.

add_action( 'gform_advancedpostcreation_post_after_creation', 'update_event_information', 10, 4 );
function update_event_information( $post_id, $feed, $entry, $form ){
	//update the All Day setting
	$all_day = $entry['9.1'];
	if ( $all_day == 'All Day' ){
		update_post_meta( $post_id, '_EventAllDay', 'yes');
	}
 
	//update the Hide From Monthly View Setting
	$hide = $entry['9.2'];
	if ( $hide == 'Hide From Event Listings') {
		update_post_meta( $post_id, '_EventHideFromUpcoming', 'yes' );
	}
 
	//update the Sticky in Month View setting
	$sticky = $entry['9.3'];
	if ( $sticky == 'Sticky in Month View' ){
		wp_update_post(array( 'ID' => $post_id, 'menu_order' => '-1' ) );
	}
	else{
		wp_update_post(array( 'ID' => $post_id, 'menu_order' => '0' ) );
	}
 
	//update the Feature Event setting
	$feature = $entry['9.4'];
	if ( $feature == 'Feature Event'){
		update_post_meta( $post_id, '_tribe_featured', '1');
	}
	else{
		update_post_meta( $post_id, '_tribe_featured', '0');
	}
}

Populating Drop Down with Event Categories

Several filters are needed to populate the drop down. The article Dynamically Populating Drop Down Or Radio Buttons Fields has more information. The filters used are:

//target form 80 (change to your form id)
add_filter( 'gform_pre_render_80', 'populate_dropdown_with_event_categories' );
add_filter( 'gform_pre_submission_filter_80', 'populate_dropdown_with_event_categories' );
add_filter( 'gform_admin_pre_render_80', 'populate_dropdown_with_event_categories' );
add_filter( 'gform_pre_validation_80', 'populate_dropdown_with_event_categories' );
function populate_dropdown_with_event_categories( $form ) {

	$event_categories = get_terms( array('taxonomy'=>'tribe_events_cat','hide_empty'=>0 ) );
	//Creating drop down item array.
	$items = array();

	//Adding initial blank value.
	$items[] = array( 'text' => '', 'value' => '' );
 
	//Adding product category terms to the items array
	foreach ( $event_categories as $event_category ) {
		$items[] = array( 'value' => $event_category->name, 'text' => $event_category->name );
	}

	//Adding items to field id 10. Replace 10 with your actual field id. You can get the field id by looking at the input name in the markup.
	foreach ( $form['fields'] as &$field ) {
		if ( $field->id == 10 ) {
			$field->choices = $items;
		}
	}

	return $form;
}

Matching up Images with Advanced Custom Fields (ACF) or other Custom Fields

When images are stored in Custom Fields, they are written into Custom Post Meta with a value of the ID (or an array of IDs) that link to the Image in the Media Library. In order to properly map an Image Custom Field to an Upload Field added from Gravity Forms, you need to use the {apc_media} merge tag in the Custom Field Mapping for the Advanced Post Creation Feed.

Make sure the Field is copied to the Media Library

The file will not be available to be mapped properly to the Custom Meta if you don’t also copy that file to the Media Library. As long as you have added your image field as a File Upload Field, it will be available to copy the field to the Media Library. This will make sure that the field has a Post ID that is the ‘Image ID’ in ACF and other Custom Field plugins.

Map the Image Field to the Custom Field

You will need to perform this step for each image field you’re adding.

  1. Under Custom Fields, Add a field mapping for your Image field. Select the ‘name’ of the field under Name.
  2. Under Value, Select “Add a Custom Value”
  3. Use the Merge Tag Tool to select your Image Field to place it’s merge tag in the Value
  4. Replace the “Field Name” in the Merge tag with the apc_media merge tag action
  5. Add ids or urls as the return type to the end of your image tag