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

  • SOLVED

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:

a:13:{s:2:"id";s:1:"6";s:13:"display_title";s:0:"";s:19:"display_description";s:0:"";s:25:"disable_woocommerce_price";s:3:"yes";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:3:"yes";s:22:"disable_label_subtotal";s:3:"yes";s:21:"disable_label_options";s:3:"yes";s:19:"disable_label_total";s:3:"yes";s:14:"label_subtotal";s:0:"";s:13:"label_options";s:0:"";s:11:"label_total";s:0:"";}

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:

s:439:"a:13:{s:2:"id";s:1:"6";s:13:"display_title";s:0:"";s:19:"display_description";s:0:"";s:25:"disable_woocommerce_price";s:3:"yes";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:3:"yes";s:22:"disable_label_subtotal";s:3:"yes";s:21:"disable_label_options";s:3:"yes";s:19:"disable_label_total";s:3:"yes";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";}";

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
maybe_unserialize()

GF Functions
gform_save_field_value


You can also look at the support request I opened with GF here:
http://www.gravityhelp.com/forums/topic/post-custom-field-default-value-altered-in-db

Answers (1)

2012-10-05

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
$s='a:13:{s:2:"id";s:1:"6";s:13:"display_title";s:0:"";s:19:"display_description";s:0:"";s:25:"disable_woocommerce_price";s:3:"yes";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:3:"yes";s:22:"disable_label_subtotal";s:3:"yes";s:21:"disable_label_options";s:3:"yes";s:19:"disable_label_total";s:3:"yes";s:14:"label_subtotal";s:0:"";s:13:"label_options";s:0:"";s:11:"label_total";s:0:"";}';

//unserialize the string
$a=unserialize($s);

//debug:
//print_r($a);

// 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:

print_r($entry);
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"]);
var_dump($post);

;-)


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
</blockquote>

http://www.gravityhelp.com/documentation/page/Gform_after_submission


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