Tagged: ,

Viewing 7 reply threads
  • Author
    Posts
    • #6627
      stham
      Member

      Hi there,

      I am building a plugin that has a Settings page. Currently, the Settings page consists of a bunch of add-more fields. Each add-more allows multiple textfields and is used to build a list of dropdown options that is used elsewhere in the system.

      Everything is working fine, but the client has now requested that each add-more field be pre-populated with a list of starter values after the plugin is activated (without the user manually entering said values).

      I have hooked a function to ‘admin_init’ (and set it to run only once) and tried using update_option() to save the values of the fields but it doesn’t appear to work. update_option() saved to the database, but the fields in the Settings page are still blank after activation.

      What function can I use to save values for the add-more fields in the Settings page?

      Thanks in advance!

      Attachments:
      You must be logged in to view attached files.
    • #6631
      Jason
      Keymaster

      Hi @stham!

      You would probably want to use the register_activation_hook instead of admin_init, otherwise you’ll be updating the settings every time the admin-side loads. Also, check to make sure there isn’t an existing value or you’ll overwrite what they’ve done if the plugin is deactivated and reactivated.

      That said, using update_option should work just fine. Just make sure you’re using the right options key and saving in the format Piklist is expecting. I’d enter the values through the field, save it, then check out the wp_options table to see how Piklist is saving it out. It may either be a single, serialized row, or multiple rows per value (depending on whether it’s a group or not).

      That should work. Piklist isn’t doing anything magical beyond the normal use of get_option and update_option.

    • #6632
      bicho44
      Member

      I think

      1) You have to take a look at the database, to see how the add-more’s are storaged.
      2) Then make sure your adding the prepopulated info in the same way. I cannot remember but i think is a JSON array in one database field.
      3) Also (a little more work) you can make a xml with the prepopulated info and import them with the wordpress import. But i think is better the other way

      Best Regards

    • #6639
      stham
      Member

      Thank you, Jason and bicho44. Those pointers were very helpful.

      After some more digging into the database, here’s what I found (in case it might help someone else):
      1. The add-mores are stored serialized.
      2. The data is stored using set_transient() based on their option_name in the wp_option table.
      3. An array of all the fields is passed through serialize() and md5(), and the resulting string forms part of the transient name.

      I haven’t quite gotten everything to work yet but I hope I’m on the right track.

      Attachments:
      You must be logged in to view attached files.
    • #6641
      stham
      Member

      Oops! Made a big mistake in my earlier response. The data IS saved with update_option(), not using transients as I previously thought.

      Sorry for the confusion. Been staring at codes for too many hours I think.

    • #6648
      Steve
      Keymaster

      @stham– You can easily set default values for add-more fields. They won’t be saved to the database until the user presses save, but they will be displayed. The value parameter in Piklist sets the default value. For Add-more fields you need to pass an array:

      'value' => array('Swimming Pool', 'Gym')

    • #6664
      stham
      Member

      Hi Steve,

      Thanks for the response. I finally managed to get everything working.

      From within register_activation_hook, I put together an array (based on the same format that I discovered in the wp_option table) and used update_option to save it. The values then showed up perfectly in the settings page.

      $defaults = array(
        'my_addmore_field_1' => array( 'Swimming Pool', 'Gym' ),
        'my_addmore_field_2' => array( 'Lorem', 'Ipsum' )
      );
      update_option( 'my_option', $defaults );

      But the defaults are a good idea too. Will keep that in mind for future reference.

      Thanks again!

    • #6665
      Steve
      Keymaster

      Glad you got it working. Closing ticket

Viewing 7 reply threads
  • The topic ‘Function to programatically save add-more field values in settings?’ is closed to new replies.