Search code examples
phpwordpressgravity-forms-plugin

Wordpress Gravity Forms Serialized List


I've got a List field with Gravity Forms to populate some custom fields in a custom post type. The site is a recipe submission site, and I'm looking for users to be able to add ingredients individually for better SEO. My issue is, when I submit the form only the last input field under ingredients is passed to the recipe.

I know I need a serialized list as this custom field pulls an array, but I'm at a complete loss of how to do that. The array should read something like this

 a:8:{i:0;s:26:"4oz piece of salmon/person";i:1;s:12:"1 egg/person";i:2;s:37:"1-2 multi-colored bell peppers/person";i:3;s:12:"Greek olives";i:4;s:9:"Olive oil";i:5;s:13:"Salt & Pepper";i:6;s:22:"Basil (fresh or dried)";i:7;s:0:"";}

I don't even know where to begin in putting together a serialized array for one form field, so any nudge in the right direction is greatly appreciated.


Solution

  • Unfortunately Gravity Forms is configured to store these as separate meta records. One option is to customize the Gravity Forms forms_model.php file, create_post function, which unserializes the field contents and loops through each item to create a new post_meta record.

    The following code should replace the case for field type list, and will prevent the creation of individual meta records on a predefined array of Gravity Form fields:

    case "list" :
        $skipped_list_fields = array('<meta name for field to skip unserializing>',
            '<meta name for another field to skip unserializing>');
        $value = maybe_unserialize($value);
        if (in_array($meta_name, $skipped_list_fields)) {
            if(!rgblank($value))
                add_post_meta($post_id, $meta_name, $value);
        } else {
            if(is_array($value)){
                foreach($value as $item){
                    if(is_array($item))
                        $item = implode("|", $item);
    
                    if(!rgblank($item))
                        add_post_meta($post_id, $meta_name, $item);
                }
            }
        }
        break;