这是转义JSON。格式很好,如下所示:
[
{
"title": "Brand Primary",
"value": "#2185D0",
"_id": "627a0637cf178d93e50be224cc07cd6e"
},
{
"title": "Brand Secondary",
"value": "transparent",
"_id": "017280d9ec94a585c2de0bee8f49d8fb"
},
{
"title": "Typo normal",
"value": "#383838",
"_id": "ff77119a11466c8d7a0efd612109fe6a"
},
{
"title": "Typo on Brand Base",
"value": "#383838",
"_id": "8a3c1eebb8067b37fc47fc505f44b8b4"
},
{
"value": "rgb(255, 255, 255)",
"title": "Typo on Brand Active",
"_id": "66fe7ced-3ef8-4dac-92c7-568d604c2931"
}
]
因此,您有一个包含标题和值的对象数组。
要更新其中的值,您需要:
获取值取消缩放(删除斜杠)将JSON解析为PHP数组在PHP数组上循环以查找具有要更改的标题的项更改该项目的值将整个数组重新编码为JSON保存值类似这样:
$color_items = get_option( \'cornerstone_color_items\' );
$color_items = stripslashes( $color_items );
$color_items = json_decode( $color_items );
foreach ( $color_items as $color_item ) {
if ( \'Brand Primary\' === $color_item->title ) {
$color_item->value = \'NEW VALUE HERE\';
break;
}
}
$color_items = wp_json_encode( $color_items );
$color_items = addslashes( $color_items );
update_option( \'cornerstone_color_items\', $color_items );