I am facing an issue in wordpress serialized data. I am developing a custom plugin which is in relation with woocommerce. I have added a checkout section in woocommerce settings section. Also I am providing same settings update form in my plugin section which is new menu option in left menu.
when I am saving data through woocommerce setting section it stores data in in wp_options table as serialized data. Below is example :
a:18:{s:7:"enabled";s:3:"yes";s:9:"test_mode";s:2:"no";s:19:"is_application_name";s:0:"";s:10:"is_api_key";s:0:"";s:17:"order_customtable";s:0:"";s:16:"order_customflds";s:0:"";s:23:"order_product_customfld";s:0:"";s:14:"is_merchant_id";s:0:"";s:5:"title";s:12:"Infusionsoft";s:9:"tax_label";s:9:"Sales Tax";s:16:"is_free_shipping";s:2:"no";s:11:"description";s:20:"Pay via Infusionsoft";s:5:"cards";s:16:"VISA MASTERCARD";s:14:"wooorderstatus";s:0:"";s:14:"thanks_message";s:39:"Thank you. Your order has been received";s:5:"debug";s:2:"no";s:11:"debug_email";s:0:"";s:13:"http_post_key";s:0:"";}
From my plugin page, on form submit I am getting field values and creating an array as below :
Array ( [enabled] => yes [test_mode] => no [is_application_name] => [is_api_key] => [order_customtable] => [order_customflds] => [order_product_customfld] => [is_merchant_id] => [title] => Infusionsoft [tax_label] => Sales Tax [is_free_shipping] => no [description] => Pay via Infusionsoft [cards] => VISA MASTERCARD [wooorderstatus] => [thanks_message] => Thank you. Your order has been received [debug] => no [debug_email] => [http_post_key] => )
Now serializing and updating option using function update_option it will save data in in data base as below string :
s:597:"a:18:{s:7:"enabled";s:3:"yes";s:9:"test_mode";s:2:"no";s:19:"is_application_name";s:0:"";s:10:"is_api_key";s:0:"";s:17:"order_customtable";s:0:"";s:16:"order_customflds";s:0:"";s:23:"order_product_customfld";s:0:"";s:14:"is_merchant_id";s:0:"";s:5:"title";s:12:"Infusionsoft";s:9:"tax_label";s:9:"Sales Tax";s:16:"is_free_shipping";s:2:"no";s:11:"description";s:20:"Pay via Infusionsoft";s:5:"cards";s:15:"VISA MASTERCARD";s:14:"wooorderstatus";s:0:"";s:14:"thanks_message";s:39:"Thank you. Your order has been received";s:5:"debug";s:2:"no";s:11:"debug_email";s:0:"";s:13:"http_post_key";s:0:"";}";
Please help me in this issue.
Don't serialize the array yourself, update_option
will do it if needed.
https://developer.wordpress.org/reference/functions/update_option/