- This topic has 7 replies, 4 voices, and was last updated 5 years, 8 months ago by
Steve.
-
AuthorPosts
-
-
June 7, 2016 at 11:07 pm #6627
sthamMemberHi 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. -
June 8, 2016 at 12:07 pm #6631
JasonKeymasterHi @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_optionshould 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_optionandupdate_option. -
June 8, 2016 at 1:30 pm #6632
bicho44MemberI 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 wayBest Regards
-
June 9, 2016 at 4:43 am #6639
sthamMemberThank 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. -
June 9, 2016 at 6:25 am #6641
sthamMemberOops! 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.
-
June 9, 2016 at 1:11 pm #6648
SteveKeymaster@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
valueparameter in Piklist sets the default value. For Add-more fields you need to pass an array:'value' => array('Swimming Pool', 'Gym') -
June 10, 2016 at 7:57 am #6664
sthamMemberHi 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 thewp_optiontable) and usedupdate_optionto 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!
-
June 10, 2016 at 8:49 am #6665
SteveKeymasterGlad you got it working. Closing ticket
-
-
AuthorPosts
- The topic ‘Function to programatically save add-more field values in settings?’ is closed to new replies.