Pregunta

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.

¿Fue útil?

Solución

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;

Otros consejos

Jason, someone had a similar situation https://stackoverflow.com/questions/20591802/how-to-save-comma-separated-inputs-in-gravity-forms-to-a-global-array-on-form-su - I think a similar edit in functions.php in your theme is the direction...

With a Custom Field form field configured as a List type and having filled out the form like this, I'm seeing all the ingredients as separate post meta items (but all attached to the same key) like this.

Is this different from what you are seeing or are you trying to achieve something different?

Use the 'List' field type from the 'Advanced Fields' area when creating/editing your form. That data is already serialized and saved as an array in a single field. Just use the gform_after_submission hook to save the GF field as postmeta.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top