0
votes

Problems with WordPress update_post_meta.

I have a function that should be updating _gravity_form_data. It works, but it is adding extra data for some reason. Here's the function:

Here is the function:

function wpufe_gravity_custom_video( $post_id ) {
    if (isset( $_POST['custom_video'])) {

        $custom_video = 'a:13:{s:2:"id";s:1:"2";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";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:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";}';

        update_post_meta( $post_id, '_gravity_form_data', $custom_video );
    }
}
add_action( 'wpuf_add_post_after_insert', 'wpufe_gravity_custom_video' );
add_action( 'wpuf_edit_post_after_update', 'wpufe_gravity_custom_video' );

It SHOULD be making _gravity_form_data have this content:

a:13:{s:2:"id";s:1:"2";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";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:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";}

Yet when it updates the _gravity_form_data field, its output is:

s:429:"a:13:{s:2:"id";s:1:"2";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";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:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";}";

Why is it adding the s:429:" in the beginning and then the extra "; at the end? And most importantly, how do I fix it? :)

Thanks so much!


UPDATE: Was suggested to serialize the data, so I did:

function wpufe_gravity_custom_video( $post_id ) {
    if (isset( $_POST['custom_video'])) {
        $gubcustom_video = array('id' => 2, 'display_title' => '', 'display_description' => '', 'disable_woocommerce_price' => 'no', 'price_before' => '', 'price_after' => '', 'disable_calculations' => 'yes', 'disable_label_subtotal' => 'no', 'disable_label_option' => 'no', 'disable_label_total' => 'no', 'label_subtotal' => 'Subtotal', 'label_options' => 'Options', 'label_total' => 'Total');
        $gubvideodata = serialize($gubcustom_video);
        update_post_meta( $post_id, '_gravity_form_data', $gubvideodata );
    }
}
add_action( 'wpuf_add_post_after_insert', 'wpufe_gravity_custom_video' );
add_action( 'wpuf_edit_post_after_update', 'wpufe_gravity_custom_video' );

...and still get the incorrect data posted to mysql.

Is there a way to just insert that string I need to insert? That's all I want to do, is update the database with that text.

I can do this in phpMySql easily, just copy and paste the text and it works like a champ. That's the solution I'm hoping for. Thanks again!

2

2 Answers

0
votes

It looks like a serialized array that you are trying to insert manually but you should insert it by serializing it using serialize function, for example:

$custom_video = array('id' => 2, 'display_title' => '');
$data = serialize($custom_video);
update_post_meta( $post_id, '_gravity_form_data', $data );

Also, try using following hook:

add_action( 'save_post', 'save_meta_data' );

function save_meta_data($post_id)
{
    $custom_video = array('id' => 2, 'display_title' => '');
    $data = serialize($custom_video);
    update_post_meta( $post_id, '_gravity_form_data', $data );
}
0
votes

Problem solved. WP User Frontend Pro was setting the meta key with its own value of "1", causing it to re-serialize the serialized string. Problem solved by removing the WPUF meta key and just leaving the field.