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.

Code to specify that a tag entered in a form should go to a custom taxonomy

  1. RichardBest
    Member

    Hi All

    I've come across a great looking piece of code that appears to enable one to specify that a tag entered into the tags field should go to a specified custom taxonomy: http://code.hyperspatial.com/104/gravity-forms-taxonomy-fix/

    Here's the code:

    <?php
    //Tag to Taxonomy - A fix for gravity forms
    
    function hyp_tag_to_taxonomy( $form ){
         $id = $form['post_id'];
         $raw_tag_list = $form['12'];
         $tag_array = explode(',', $raw_tag_list);
    
         foreach ($tag_array as $tag){
              wp_set_object_terms( $id, $tag, 'collective',true);
         }
    }
    
    add_action( 'gform_post_submission', 'hyp_tag_to_taxonomy' );
    
    ?>

    My question please is whether this can be done for multiple different taxonomies across different forms on a single site, i.e.,:

    (1) Can it be done for two different tag fields in a single form, with tags entered into the first tag field going to WP's default tags taxonomy, and tags entered into the second tag field going to a custom taxonomy?

    (2) Can it be done across multiple different forms?

    Any help on this one much appreciated. The ability to slice and dice taxonomies in this way would be fantastic.

    Cheers
    Richard

    Posted 12 years ago on Monday September 19, 2011 | Permalink
  2. RichardBest
    Member

    P.S. I've tried the code Alex suggested in another forum post (http://www.gravityhelp.com/forums/topic/custom-taxonomies) but couldn't get it working. It seems to be pulling the tags from the default WP tags field and not posting any tags to any taxonomy. The code I tried for this was as follows:

    /*-----------------------------------------------------------------------------------*/
    /*	Add Support for Posting of Tags in Matters Form to multiple custom taxonomies
    /*-----------------------------------------------------------------------------------*/
    
    add_filter("gform_post_data", "empty_tags", 10, 2);
    function empty_tags($post_data, $form){
        //replace 4 with your actual form id
        if($form["id"] !=9)
            return;
    
        $post_data["tags_input"] = null;
        return $post_data;
    }
    
    add_filter("gform_post_submission", "insert_custom_taxonomies", 10, 2);
    function insert_custom_taxonomies($entry, $form){
    
        //REPLACE 4 with your actual form id
        if($form["id"] !=9)
            return;
    
        //getting post
        $post_id = $entry["post_id"];
    
        //REPLACE 2 with your actual field id and "post_tag" with your custom taxonomy
        wp_set_object_terms($post_id, explode(",", $entry[9]), "projects");
    /*-----------------------------------------------------------------------------------*/
    /*    wp_set_object_terms($post_id, explode(",", $entry[3]), "activities");
    
    /*-----------------------------------------------------------------------------------*/
    }

    In this example, my form ID is 9 and the tags field that I'm trying to use to populate a different taxonomy is also 9.

    Posted 12 years ago on Monday September 19, 2011 | Permalink
  3. RichardBest
    Member

    I've also just tried this using a single line field instead of a tags field to take the incoming entry I'm trying to get into the custom taxonomy, but nada.

    Posted 12 years ago on Monday September 19, 2011 | Permalink
  4. I'll have someone take a look at this code and get back with you tomorrow. As with anything involving Gravity Forms pretty much anything is possible so I have no doubt you can do this, just a matter of tweaking the code.

    Posted 12 years ago on Tuesday September 20, 2011 | Permalink
  5. Hi Richard,

    Something like this would allow you to specify different fields to assign the tags to different taxonomies. This is a manual approach.

    http://pastie.org/2565508

    Posted 12 years ago on Tuesday September 20, 2011 | Permalink
  6. RichardBest
    Member

    Hi and many thanks David. Much appreciated.

    Am I right in thinking though that this code doesn't allow one to differentiate between different forms? In my case, I have a site with 5 forms on it and I need to take this approach across the different forms for different audiences.

    Thanks again
    Richard

    Posted 12 years ago on Wednesday September 21, 2011 | Permalink
  7. That code isn't form specific. If you want to execute it form specific then you'd append the form id to the end of the gform_post_submission so it's gform_post_submission_ID just like any other hook you want to make form specific.

    Posted 12 years ago on Wednesday September 21, 2011 | Permalink
  8. Hi Richard,

    You can wrap this switch in another switch for the form ID with a case for each form.

    Psuedo Code

    [php]
    switch($form_id) {
    case 1:
        switch($field_id) {
        case 1:
            // code
            break;
        case 2:
            // code
            break;
        }
    case 2:
        switch($field_id) {
        case 3:
            // code
            break;
        case 4:
            // code
            break;
        }
    }
    Posted 12 years ago on Wednesday September 21, 2011 | Permalink
  9. RichardBest
    Member

    Hi and many thanks David. Apologies if this is an ignorant question, but presumably I would replace case "1" at line 2 with the number of the form I want to target, e.g.,
    "case 7", and likewise for case 2 at line 11 etc?

    Thanks for your help.
    Richard

    Posted 12 years ago on Thursday September 22, 2011 | Permalink
  10. RichardBest
    Member

    P.S. If that's right, would the complete code be:

    <?php
    
    add_action('gform_post_submission', 'hyp_tag_to_taxonomy', 10, 2);
    function hyp_tag_to_taxonomy($entry, $form){
    
        $post_id = $entry['post_id'];
        $taxonomy = '';
    
    switch($form_id) {
    case 1: // update to form ID
    
        switch($field_id) {
        case 5: // update to a field ID which contains a list of tags
                $taxonomy = 'your-tax-name';
                $tag_string = $entry[5];
                break;
        case 6: // update to a field ID which contains a list of tags
                $taxonomy = 'different-tax-name';
                $tag_string = $entry[6];
                break;
        }
    
    case 2: // update to form ID
    
        switch($field_id) {
        case 7: // update to a field ID which contains a list of tags
                $taxonomy = 'your-tax-name';
                $tag_string = $entry[7];
                break;
        case 8: // update to a field ID which contains a list of tags
                $taxonomy = 'different-tax-name';
                $tag_string = $entry[8];
                break;
        }
    
    $tags = array_map('trim', explode(',', $tag_string));
    
            if(!$post_id || empty($tags))
                return;
    
            foreach($tags as $tag)
                wp_set_object_terms($id, $tag, $taxonomy, true);
    
    }

    Putting the two chunks of code together proves to be more challenging than 1+1 (for me at least). :)

    Posted 12 years ago on Thursday September 22, 2011 | Permalink
  11. RichardBest
    Member

    Hi - Would be great if someone could let me know if I have this code right please. I'm keen not to stuff things up... :)

    Posted 12 years ago on Thursday September 22, 2011 | Permalink
  12. RichardBest
    Member

    Hi GF team - No problem if you're too busy, but it'd be great for confirmation please as to whether my code attempt above is good or bad (I suspect the latter) before the weekend so I can continue work on the site over the weekend.

    As always, thanks for the super support.
    Richard

    Posted 12 years ago on Friday September 23, 2011 | Permalink
  13. David is out sick today, so you will have to wait until he returns and he can take a look at the code.

    Posted 12 years ago on Friday September 23, 2011 | Permalink
  14. RichardBest
    Member

    Thanks Carl. No problem. Sorry to hear you're crook David.

    Posted 12 years ago on Friday September 23, 2011 | Permalink
  15. RichardBest
    Member

    Hi All - In case it helps, I had a developer look at combining the two bits of code referred to above. He produced the following but it didn't work:

    // Adding ability to direct tags to specified taxonomies across different forms
    
    add_action('gform_post_submission', 'hyp_tag_to_taxonomy', 10, 2);
    function hyp_tag_to_taxonomy($entry, $form){
    
        $post_id = $entry['post_id'];
        $taxonomy = '';
    
        foreach($form['fields'] as $field) {
            switch($form['id']) {
            case 16: // enter your form ID here
               switch($field['id']) {
               case 3: // update to a field ID which contains a list of tags
                  $taxonomy = 'Projects';
                  $tag_string = $entry[3];
                  break;
               case 4: // update to a field ID which contains a list of tags
                  $taxonomy = 'Status';
                  $tag_string = $entry[4];
                  break;
               }
               break;
            case 17: // enter your form ID here
               switch($field['id']) {
               case 13: // update to a field ID which contains a list of tags
                  $taxonomy = 'your-tax-name';
                  $tag_string = $entry[12];
                  break;
               case 15: // update to a field ID which contains a list of tags
                  $taxonomy = 'different-tax-name';
                  $tag_string = $entry[14];
                  break;
               }
               break;
            }
    
            $tags = array_map('trim', explode(',', $tag_string));
    
            if(!$post_id || empty($tags))
                return;
    
            foreach($tags as $tag)
                wp_set_object_terms($id, $tag, $taxonomy, true);
    
        }
    
    }
    Posted 12 years ago on Tuesday September 27, 2011 | Permalink
  16. RichardBest
    Member

    So here's another update. There was an error of some sort in the code above. The code below ALMOST works but not 100% as intended. It DOES now assign tags to designated taxonomies but it ALSO assigns them the WP default tag taxonomy.

    If I could get the code to assign tags in the specified fields ONLY to the designated taxonomies, it would be working perfectly. (The WP default tag taxonomy would still need to work for other tag fields not referred to in the code.)

    add_action('gform_post_submission', 'hyp_tag_to_taxonomy', 10, 2);
    function hyp_tag_to_taxonomy($entry, $form){
    
        $post_id = $entry['post_id'];
        $taxonomy = '';
    
        foreach($form['fields'] as $field) {
            switch($form['id']) {
            case 1: // enter your form ID here
               switch($field['id']) {
               case 12: // update to a field ID which contains a list of tags
                  $taxonomy = 'your-tax-name';
                  $tag_string = $entry[12];
                  break;
               case 14: // update to a field ID which contains a list of tags
                  $taxonomy = 'different-tax-name';
                  $tag_string = $entry[14];
                  break;
               }
               break;
            case 2: // enter your form ID here
               switch($field['id']) {
               case 13: // update to a field ID which contains a list of tags
                  $taxonomy = 'your-tax-name';
                  $tag_string = $entry[12];
                  break;
               case 15: // update to a field ID which contains a list of tags
                  $taxonomy = 'different-tax-name';
                  $tag_string = $entry[14];
                  break;
               }
               break;
            }
    
            $tags = array_map('trim', explode(',', $tag_string));
    
            if(!$post_id || empty($tags))
                return;
    
            foreach($tags as $tag)
                wp_set_object_terms($post_id, $tag, $taxonomy, true);
    
        }
    
    }
    Posted 12 years ago on Tuesday September 27, 2011 | Permalink
  17. Hi Richard,

    Are you using Gravity Form Post Tag fields for these field types? If so, try using just a single line text input.

    Posted 12 years ago on Tuesday September 27, 2011 | Permalink