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.

Is there a way of adding settings fields dynamically?

I have my own Settings API options generator just like in Chip Bennet's Oenology Theme, everything works perfect but I can't find a way to create dynamic fields.

Here's how I add option fields.

1. First I describe all fields:

 function my_options() {
    $options = array(
         array(
            'id' => '1',
            'title' => '1', 
            'type' => 'foo',
            'description' =>  'bar',
        ),
          array(
            'id' => '2',
            'title' => '2', 
            'type' => 'foo',
            'description' =>  'bar',
        ),
    );

   return $options;
 }

2. Then I generate fields:

foreach(my_options() as $field) {
    add_settings_field(
       $field['id'],
       $field['title'],
       "something",
       "else",
       "goes",
       "there"
    );
}

Then doing:

   <?php $opts = get_option('my_theme_settings_api'); 
   var_dump($opts); ?>

Returns all the fields.

But what If want to generate some of the fields "on the fly", eg. based on some other array, just like that:

function my_options() {

   // $myarray = some array of elements taken from different source, like json data from other website etc.
   $count = 0;
   foreach($myarray as $something) {
        $count++;
        $options[] = array(
            'id' => 'something'. $count,
            'title' => $something['title'],
            'type' => 'type',
            'desription' => $something['description'],
        );

   return $options;
 }

Then I'm getting the fields displayed using do_settings_sections() but I can't save them on admin pages and when I do var_dump($opts); just like in example above these options doesn't exist there.

Any clues?

share|improve this question

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.