IntroductionCreating Products for WooCommerceCreate the FormCreate the Advanced Post Creation FeedHandling Fields Unable to be MappedPriceCategoryPopulating Drop Down with Product CategoriesCreating Events for The Events CalendarCreate the FormCreate the Advanced Post Creation FeedHandling Fields Unable to be MappedPopulating Drop Down with Event CategoriesMatching up Images with Advanced Custom Fields (ACF) or other Custom FieldsMake sure the Field is copied to the Media LibraryMap the Image Field to the Custom Field
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 SettingsDrop DownCategorySee Populating Drop Down with Product Categories below for details.Single Line TextProduct Name ParagraphDescription NumberPriceSet the number format to currencyFile 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.
Expand the drop down for Type in the Post Settings section and select Product.
Scroll to the Post Content section and map the Title to the Product Name field.Map the Content to the Description field.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.
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 article 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:
gform_pre_rendergform_pre_submission_filtergform_admin_pre_rendergform_pre_validation
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 SettingsDrop 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 EventDateStart Date TimeStart TimeConditional Logic: Show field if Event Options is not All DayDateEnd 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.
Expand the drop down for Type in the Post Settings section and select Event.
Scroll to the Post Content section and map the Title to the Event Title field.Map the Content to the Event Description field.Scroll to the Taxonomies section and map the Event Categories to Field Value and Category field.
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 for several fields which need to be handled specially.
For more details about the hook parameters, placement, and usage, see the article mentioned above.
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:
gform_pre_rendergform_pre_submission_filtergform_admin_pre_rendergform_pre_validation
//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 to Custom Fields, they are written into Custom Post Meta with a value of the ID (or an array of id』s) 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.
Under Custom Fields, Add a field mapping for your Image field. Select the 『name』 of the field under Name.Under Value, Select 「Add a Custom Value」Use the Merge Tag Tool to select your Image Field to place it』s merge tag in the ValueReplace the 「Field Name」 in the Merge tag with the apc_media merge tag actionAdd ids or urls as the return type to the end of your image tag