Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Unserialize Gform Field Value before Saving to DB WordPress


I am using GF to create products from the frontend for WooCo. All my WooCo products use the Gravity Forms Add-on and all use the same settings.

When a product cpt is created it has a custom field that stores the settings for the gf add-on. So I would like to define this custom field so I can standardize product creation.

This is how the custom field (the custom field is _gravity_form_data) value should look:


As you can see, this is already serialized.

When I tried to insert that value as the 'default value' in the form it becomes this when being saved in the DB:


It is being reserialized by GF with s:430:" "; wrapped around it.

There are a few tools out there, but I was not able to get any of them to work. Hoping someone else can

WP Functions

GF Functions

You can also look at the support request I opened with GF here:

Answers (1)


Dbranes answers:

it sounds like you need to save the value as array not string.

Maybe the action "gform_post_submission" is helpful

add_action("gform_post_submission", "save_array", 10, 2);
function save_array($entry, $form){
$post_id = $entry["post_id"];
$form_id = $entry["form_id"];

// only use it for some specific form (fx. id 2)
if ($form_id==2){

//your serialized string

//unserialize the string


// update some custom field with the correct array value:
update_post_meta($post_id, "_gravity_form_data", $a);

Kyle comments:

I don't think update_post_meta will work because the form is creating the post

Dbranes comments:

hi, the "gform_post_submission" hook runs after entry data has been saved
and it should have access to the submitted form data.

you could try to debug inside the above hook to check if the data has been saved, fx. with:

var_dump(get_post_meta($post_id, "_gravity_form_data"));
var_dump($post = get_post($entry["post_id"]));

ps: it looks like "gform_save_field_value" can only save string but not array values,
that's why I suggested update_post_meta since it can handle array values ;-)

Dbranes comments:

ps: mistake in the last comment:

var_dump($post = get_post($entry["post_id"]));

should be written:

$post = get_post($entry["post_id"]);


Dbranes comments:

ps: there is also the "gform_after_submission" action hook:

add_action("gform_after_submission", "after_submission", 10, 2);

<blockquote>This action hook is executed at the end of the submission process (after form validation, notification, and entry creation). Use this hook to perform actions after the entry has been created

Kyle comments:

Hey, thanks for the reply.

I tried your function and played around with it, but it gave me the same result

Kyle comments:

After a few more tries it worked! I had made an error putting in your function the first time.

Thanks for the help! much appreciated