I'm adding a settings section to a theme that will allow the user to enter testimonials which will be randomly displayed on the site. Each testimonial has 2 fields, author
and testimonial
, I've registered my settings as below:
register_setting( 'settings-website', 'option_testimonials', 'save_testimonials' );
And I want to store an array within that option like this:
array(3) {
[0]=>
array(2) {
["author"]=>
string(8) "Person A"
["testimonial"]=>
string(14) "Great website!"
}
[1]=>
array(2) {
["author"]=>
string(8) "Person B"
["testimonial"]=>
string(15) "Excellent work!"
}
[2]=>
array(2) {
["author"]=>
string(8) "Person C"
["testimonial"]=>
string(10) "Brilliant!"
}
}
My options form looks like this:
<?php
$testimonials = get_option('option_testimonials');
if( !is_array($testimonials) )
$testimonials = array();
?>
<div class="wrap">
<h2>Testimonials</h2>
<form method="post" action="options.php">
<?php settings_fields( 'ddtheme-settings-testimonials' ); ?>
<table class="form-table">
<?php foreach( $testimonials as $testimonial ) { ?>
<tr valign="top">
<th scope="row">Testimonial</th>
<td>
</td>
</tr>
<?php } ?>
<tr valign="top">
<th scope="row">New testimonial</th>
<td>
<label>Author:</label><br/>
<input type="text" name="author" style="width: 400px;" /><br/>
<label>Testimonial:</label><br/>
<textarea name="testimonial" style="width: 400px;"></textarea>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
So I thought I could grab the input from the author
and testimonial
fields inside save_testimonials
, combine them into an array and append that to the existing array and save but the $_POST
and $_REQUEST
are both empty inside that callback.
How can I do this?