0

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?

1 Answer 1

0

You'd probably be best to use add_settings_field and add_settings_section...

But in any case, save_testimonials is your validation / save callback function. It is given the data from the form and its job is to check it and then return the validated options. You shouldn't be touching $_POST or $_REQUEST - that's the point of the settings api.

save_callback{$array_of_data_from_form){

     //Validate the $array_of_data_from_form 

     $validated=array()//Your settings array after validation

    return $validated;
}`

But it will only recieve data with input name option_testimonials as registered in your register_setting. So your input fields will need names like option_testimonials[testimonial] and option_testimonials[author].

Best practise dictates that you should prefix your option names (option_testimonials is a bit too generic).

As far tutorials go concerning the settings API you can't go far wrong with this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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