PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamic add_action creation?

  1. sascha
    Member

    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?

    Posted 10 years ago on Tuesday January 17, 2012 | Permalink
  2. sascha
    Member

    Tried to above, but of course it does not work! Would I need to call a add_action('gform_after_submission', 'filter_forms_by_id') and then inside that function filter_forms_by_id do the stuff I suggested above? I cannot get my head round it. Sorry. Maybe you can...

    Posted 10 years ago on Tuesday January 17, 2012 | Permalink
  3. sascha
    Member

    In case it helps someone, I used the following code and it seems to work. But maybe one of your GF guys could look over it :) I am not a php man....

    //add_action('gform_after_submission', 'dynamic_filtering', 10, 1);
    
    //function dynamic_filtering() {
    $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) {
    	//$after_submission = 'gform_after_submission_' . $form_id;
         add_action('gform_after_submission_' . $form_id, 'manitu', 10, 1);
    }
    //}
    
    function manitu($entry) {
    	do_dump ($entry);
    }

    As you can see I made a mistake with the array declaration in the example before! That's why it would not work. Yeah, and I forgot the brackets around the foreach argument...

    Posted 10 years ago on Tuesday January 17, 2012 | Permalink