0

How do I save an array of objects in Wordpress/PHP via update_option ?

What I'm trying to save is something like:

update_option('my_option', [ 
 { 
  'c': htmlspecialchars($_POST['hidden_category']), 
  't': htmlspecialchars($_POST['hidden_tags']) 
 }, 
 { 
  'c': htmlspecialchars($_POST['hidden_category_1']), 
  't': htmlspecialchars($_POST['hidden_tags_1']) 
 },
 //etc..
]);

$_POST['hidden_category'] contains a string

while $_POST['hidden_tags'] contains an array of strings

5
  • 1
    If I understood you correctly, I would apply one of functions serialize() or json_encode() to a value of $_POST['hidden_tags']. Commented Oct 6, 2022 at 22:07
  • I see, but the js like [{ .. }, { .. }, { .. }] structure itself is something update_option/php/mysql accepts? Commented Oct 6, 2022 at 22:10
  • Mentioned functions are returns a string result that you can store in MySQL. When you need to use a stored value you should deserialize() or json_decode(). Commented Oct 6, 2022 at 22:16
  • I'm trying, but var_dump(json_decode(get_option( 'my_option' )[0])) returns null with a var_dump , but get_option( 'my_option' ) var_dumps array(1) { [0]=> string(112) "{c: "MyCategory", t: "tag1, tag2, tag3" }" } Commented Oct 7, 2022 at 0:08
  • @Bob thanks I found the issue, it was in the format of the json, now it works Commented Oct 7, 2022 at 1:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.