WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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(
            'id' => '1',
            'title' => '1', 
            'type' => 'foo',
            'description' =>  'bar',
            'id' => '2',
            'title' => '2', 
            'type' => 'foo',
            'description' =>  'bar',

   return $options;

2. Then I generate fields:

foreach(my_options() as $field) {

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) {
        $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


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

Browse other questions tagged or ask your own question.