wordpress中的序列化数据问题

Serialized data issue in wordpress

我在 wordpress 序列化数据中遇到问题。我正在开发一个与 woocommerce 相关的自定义插件。我在 woocommerce 设置部分添加了结帐部分。此外,我在我的插件部分提供了相同的设置更新表单,这是左侧菜单中的新菜单选项。

当我通过 woocommerce 设置部分保存数据时,它将数据存储在 wp_options table 中作为序列化数据。以下是示例:

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:"";}

从我的插件页面,在提交表单时,我正在获取字段值并创建一个数组,如下所示:

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] => )

现在使用函数 update_option 序列化和更新选项,它会将数据保存在数据库中,如下字符串:

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:"";}";

请帮我解决这个问题。

不要自己序列化数组,update_option 会在需要时进行序列化。

https://developer.wordpress.org/reference/functions/update_option/