update a value in wp_options [closed]

This is escaped JSON. Formatted nicely, it looks like this:

[
  {
    "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"
  }
]

So you have an array of objects that contain titles and values.

To update a value inside it you need to:

  1. Get the value.
  2. Unescape it (remove the slashes).
  3. Parse the JSON into a PHP array.
  4. Loop over the PHP array to find the item with the title that you want to change.
  5. Change the value of that item.
  6. Re-encode the whole array as JSON.
  7. Save the value.

So something like this:

$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 );