0

hey guys i'm trying to import my plugin option into wordpress with php but i can't find any tutorial or describe about this section

i have this example:

my option name:

get_option('meow');

what i have in this option are

'cats'=> '2',
'food' => array('tona','fish'),

so how can i use

update_option

to import this values? i tried this way but it's not working

update_option('meow', 'cats','5');

1 Answer 1

0

Just pass the array as the second argument to update_option - WordPress will serialize the value before storing it in the database, and automatically unserialize it when you read it:

update_option( 'meow', array(
    'cats' => '2',
    'food' => array( 'tona', 'fish' ),
) );

$meow = get_option( 'meow' );

print_r( $meow );

// Array
// (
//     [cats] => 2
//     [food] => Array
//         (
//             [0] => tona
//             [1] => fish
//         )
// )
2
  • thank you for answer, I tried the code but i have got Error: Warning: Illegal offset type in isset or empty in C:\myweb\wp-includes\option.php on line and Notice: Array to string conversion in C:\myweb \wp-includes\option.php on line AND Notice: Array to string conversion in C:\Mywebwp-includes\formatting.php on line
    – Ahmed java
    Jun 14, 2022 at 16:06
  • It's worked i just have to check if option exists or not, thanks
    – Ahmed java
    Jun 14, 2022 at 16:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.