Hi there,
I was wondering if the below stuff could be optimised? I am sure I have seen somewhere a "trick" where the _1, _3, _4 etc. gform_after_submission is called dynamically, but cannot find it.
// select parent term automatically for the 'project_type' taxonomy in form #1, 3, 4, 5, 6, 8
add_action('gform_after_submission_1', 'gform_parent_category');
add_action('gform_after_submission_3', 'gform_parent_category');
add_action('gform_after_submission_4', 'gform_parent_category');
add_action('gform_after_submission_5', 'gform_parent_category');
add_action('gform_after_submission_6', 'gform_parent_category');
add_action('gform_after_submission_8', 'gform_parent_category');
function gform_parent_category($entry) {
$created_post = $entry['post_id'];
$child_ID = $entry["5"]; // need to change this to pull dynamically
$term_object = get_term_by('id', $child_ID, 'project_type'); // replace project_type by chosen taxonomy
$parent_ID = $term_object -> parent;
wp_set_post_terms( $created_post, $parent_ID, 'project_type', true ); //replace project_type by chosen taxonomy
}
I would basically like to define the array of form ids first and then loop through creating the necessary add_action calls dynamically:
$forms_to_filter = array('1, 2, 3, 4, 5, 6); // ids of forms --- needs to be populated dynamically!
foreach $forms_to_filter as $form_id {
add_action('gform_after_submission_' . $form_id, 'gform_parent_category');
}
function gform_parent_category($entry) {
$created_post = $entry['post_id'];
$child_ID = $entry["5"]; // need to change this to pull dynamically
$term_object = get_term_by('id', $child_ID, 'project_type'); // replace project_type by chosen taxonomy
$parent_ID = $term_object -> parent;
wp_set_post_terms( $created_post, $parent_ID, 'project_type', true ); //replace project_type by chosen taxonomy
}
I am not sure at all about the right syntax for doing this though!!! Is there a way to do this? Is the above correct?