2

What is the best way to add an option value to a registered option in wp_options that is an array? With phpmyadmin i see the option_value field is

a:9:{i:3;s:15:"value1";i:6;s:5:"value2";i:7;s:21:"value3";i:8;s:15:"value4";i:9;s:7:"value5";i:10;s:4:"value6";i:11;s:5:"value7";i:12;s:8:"value8";i:13;s:7:"value9";}

How can i add value10? update_option will replace the whole option_value, right?

5

The option you're showing in your question is a serialized array. Retrieving the option with get_option() gives you back the array, but unserialized. This is done by maybe_unserialize(), which get_option uses. Just add a new 'key' => 'value' pair to the array you retrieved and then update the option with update_option(), et voilà you have added the additional value to your option.


As said in the comment can't see why it wouldn't work and as mentioned, here is some exemplary code that definitely works:

// this is just for proof of concept   
// add new data, we don't want to temper with the original
// it is an array to resemble your case
$new_new_arr = array(
    'key01' => 'value01',
    'key02' => 'value02'
);
// debug the data
print_r( $new_new_arr );
// now we actually add it tp wp_options
add_option( 'new_new_opt', $new_new_arr );
// lets get back what we added
$get_new_new_arr = get_option( 'new_new_opt' );
// debug the data
print_r( $get_new_new_arr );
// lets create the second array with the additional key => value pair
$add_new_new_opt = array( 'key03' => 'value03' );
// debug the data
print_r( $add_new_new_opt );
// lets combine, merge the arrays
$upd_new_new_arr = array_merge( $get_new_new_arr, $add_new_new_opt );
// debug the data
print_r( $upd_new_new_arr );
// now update our option
update_option( 'new_new_opt', $upd_new_new_arr );
// get back the updated option
$get_upd_new_new_arr = get_option( 'new_new_opt' );
// debug the data
print_r( $get_upd_new_new_arr );
// lets cleanup and delete the option
delete_option( 'new_new_opt' );
  • hi ialocin, i tried the following i just can't get the update_option $valid_input = get_option( 'extra_columns' ); $input = array('notes'); //exttra option field $valid_input = array_merge( $valid_input, $input ); print_r($valid_input);//seems to be added update_option( 'extra_columns', $valid_input )//not working – alex Nov 14 '14 at 20:53
  • hmm, nevermind my code seemed to work after restart tx have a nice weekend – alex Nov 14 '14 at 21:20
  • My pleasure. I was about to say, I can't see anything wrong with that. Actually I've already written some exemplary code for you. @alex Don't forget to accept the answer :) – Nicolai Nov 14 '14 at 21:23
  • 1
    @alex please explain why you are ignoring requests to accept answer which solved your issues. We all offer our spare time to help you to solve your issues but you simply ignore any proper requests to accept our answers. I think it is time we all ignore your questions. I'm not being rude, but this is ridiculous. You are misusing our time. And not only that, your total ignorance and arrogance means that your question stays unresolved. Please be a game player and take others into consideration. Thanks you :-) – Pieter Goosen Mar 26 '15 at 9:05
  • Don't know if your tone is good, although it is understandable :) - but in any way, it takes nothing away from the valid points you make. @PieterGoosen – Nicolai Mar 26 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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