I have a form on my front-page. It sends the entered address to Google API and returns the JSON to wp_options. That works fine. I am using update_option to do that, so it rewrites the meta_key each time. How would I go about having each address entered remain?

  $address = esc_html( $_POST['address'] );

  //assign JSON object to var using  geocodeing function('address') input 
  $address_results = gmap_geocode($address);

  // transfer data to options array
  $options['py_name'] = $py_username;
  $options['address'] = $address;
  $options['coords']  = $address_results;

  // create and name the option_name and set fields with $options array
  update_option( 'row_name', $options );
  • It is considered bad practice to save non-settings data in the wp_options table because usually the whole table is loaded into memory as it is the settings. If you have a lot of submissions, you could run out of runtime memory. The good practice is to create a new post type and then save each submission as a new post with the various fields saved as post meta. – Seamus Leahy Dec 7 '13 at 3:23

Load the existing option first and save the new data as an element in an array:

// get the option
$data = get_option( 'row_name' );
// add new data to the option
$data[] = $options;
// save it back to the db
update_option( 'row_name', $data );

Your option will then be an array of arrays instead of just a single array:

Array
(
    [0] => Array
        (
            [py_name] => username
            [address] => addy
            [coords] => results
        )
    [1] => Array
        (
            [py_name] => username
            [address] => addy
            [coords] => results
        )
)
  • I did it like this: ``` update_option( 'py_menu_item_' . sanitize_key( $py_username ), $options ); ``` This seems to do the trick, it gives a unique row_name each time now. – user56258 Dec 6 '13 at 21:21
  • 2
    @user56258 but then you have to query for each row individually which is far less efficient. you could key the array to username like $data[$py_username] = $options in the above example and send just a single query to the database to fetch your data. – Milo Dec 6 '13 at 22:03

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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