storing a plugin option as an array
-
I am writing a plugin, and I have an options page for it. The options page has a textarea input.
I’d like the input from the textarea to be stored as an array, with each line as a separate value (a string of text). How do I do that?
Also, do I need to sanitize this input, or does WordPress do that?
-
Anything that uses the built-in
update_option()
andget_option()
functions will serialise and un-serialise arrays and objects as required, as well as sanitising the values. You don’t need to do anything yourself.The reason I want to store the input as an array, with each line as a separate value, is that I want to use a
foreach
loop to place each value into a list item.Can
get_option()
do that?this is the code I am using to create my options page. It works, and saves the list in the textarea.
// add menu to admin if (is_admin()){ add_action('admin_menu', 'paypal_programs_menu'); add_action('admin_init', 'paypal_programs_register'); } // admin page details function paypal_programs_menu() { add_options_page('Paypal Programs Settings', 'Paypal Programs', 'manage_options', 'paypal_programs', 'paypal_programs_options'); } // add actual admin page function paypal_programs_options() { ?> <div class="wrap"> <h2>Paypal Programs</h2> <p>Edit a list of programs for the Paypal Donation form.</p> <form method="post" action="options.php"> <?php settings_fields('paypal_programs_optiongroup'); ?> <table class="form-table" style="margin-top: 20px; padding-bottom: 10px; border: 1px dotted #bbb; border-width:1px 0;"> <tr valign="top"> <th scope="row" style="padding-bottom:0"><h4>Enter each program on a separate line</h4></th> </tr><tr> <td style="padding-top:0"><textarea name="program_list" style="height:10em;width:25em"><?php echo get_option('program_list'); ?></textarea></td> </tr> </table> <?php submit_button(); ?> </form> </div> <?php } // whitelist options function paypal_programs_register() { register_setting('paypal_programs_optiongroup', 'program_list'); }
do I add
update_option()
somewhere?What I would like to output is something like
$programList = get_option('program_list'); echo '<ul>'; foreach ($programList as $program) { echo '<li>' . $program . '</li>'; } echo '</ul>';
But first I need to convert $programList to an array I think.
Where does that code actually save the values? Where ever that is you would just need to add
update_option('program_list', $program_list);
and that will save the value. Then useget_value('program_list');
when you need to get the setting.I think the WP function
submit_button()
saves the form data. I can try addingupdate_option()
under that.No, it doesn’t.
submit_button()
outputs a button, that’s all. There’s got to be somewhere else that is doing the saving. But, if you’re getting it throughget_option()
as you are now, it’s got to be saving somewhere, and should be usingupdate_option()
or maybeadd_option()
?If I put
update_option()
below thesettings_fields()
line, the options does not save.Your call to
update_option()
has to be somewhere that has access to the POST values that were sent by the form. I have to say that I’m not to sure about the settings API as I haven’t used that before. Everything that I’ve done has bene through the functions that I’ve told you about, and from what I can see on a very quick look they work differently to the settings API.Ok, lets go back to the beginning…
You said that the value is saving, so you’re obviously doing that part right. That also means that you’re outputting the value to the text field again correctly, doesn’t it?
If that’s right, what is the actual value that you’re getting back when you call
get_option()
for that value, and how is it supposed to be split up to use in aforeach()
loop?Right, I can save the value, and if I re-load the options page, the value is still in the text area, split into multiple lines, just as I entered them.
But, if I call the value with get_option() in a function, I only get a long string of text, with no line breaks. So, I can’t use the value in a foreach loop to make a list in html.
I need to save the option as an array in the admin page, but I can’t find out how to do that. The Codex is kinda thin for update_option().
Ah OK. That will make things easier. And also, text in a text area is always stored as plain text, not as an array, so even doing it this way wouldn’t make a difference to what you’d be getting from your settings.
Leave your code the way that it is, as it’s working fine saving and getting the value. No need to touch that.
All you need to do is add this in where you want to split the value into an array with one line per entry:
$program_list = explode (PHP_EOL, $$program_list);
That will split the long sting up at each line ending.
After all that… it turned out to be easier then I thought!
Hey, that does work.. thanks!
To wrap up, here is the code I used to output my data in my plugin:
$program_list = get_option('program_list'); $program_array = explode( PHP_EOL, $program_list ); echo '<ul>'; foreach ($program_array as $program) { echo '<li>' . $program . '</li>'; } echo '</ul>';
- The topic ‘storing a plugin option as an array’ is closed to new replies.