Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I save options as an array using forms? I don't want to use Settings API.

Here's working form that saves 3 options separately:

<form method="post" enctype="multipart/form-data" action="options.php">
   <?php wp_nonce_field('update-options'); ?>

   <input type="text" name="option1" value="<?php echo get_option('option1');  ?>" />
   <input type="text" name="option2" value="<?php echo get_option('option2'); ?>" />
   <input type="text" name="option3" value="<?php echo get_option('option3'); ?>" />

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="option1,option2,option3" />
<p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>
</form>

And here's how it should look for an array, but this method doesn't work (I guess the problem lies in input called page_options:

<form method="post" enctype="multipart/form-data" action="options.php">
   <?php wp_nonce_field('update-options'); ?>
   <?php $options = get_option('my_options'); ?>

   <input type="text" name="option1" value="<?php echo $options['option1']; ?>" />
   <input type="text" name="option2" value="<?php echo $options['option2']; ?>" />
   <input type="text" name="option3" value="<?php echo $options['option3']; ?>" />

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="my_options" />
<p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>
</form>

Any ideas?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Have you tried...

 <input type="text" name="my_options[option1]" value="<?php echo $options['option1']; ?>" />
 <input type="text" name="my_options[option2]" value="<?php echo $options['option2']; ?>" />
 <input type="text" name="my_options[option3]" value="<?php echo $options['option3']; ?>" />?

I should say though (and maybe you have), that you should register your settings and perform necessary validation checks on the input - or if you really don't want to use the Settings API, disregard it completely and 'manually' collect the form data, check the data, check nonces etc and add to the database.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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