0

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 );
1
  • 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. Dec 7, 2013 at 3:23

1 Answer 1

1

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
        )
)
2
  • 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, 2013 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, 2013 at 22:03

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.