Title: Dynamically create fields
Last modified: August 24, 2016

---

# Dynamically create fields

 *  Resolved [Javier](https://wordpress.org/support/users/xarkitu/)
 * (@xarkitu)
 * [10 years, 12 months ago](https://wordpress.org/support/topic/dynamically-create-fields/)
 * Hi miunosoft,
 * I have the need and the doubt as dynamically add fields.
 * If I want to dynamically create fields, for example:
 *     ```
       $ArrayFields = array(
                          'label'             => 'Ananas',
                          'default'           => 'a'
                       );
   
       $this->addSettingFields(
               array(
                   'field_id'          => 'sortable_labels',
                   'type'              => 'hidden',
                   'title'             => __( 'Sortale Labels', 'admin-page-framework-example' ),
                   'sortable'          => true,
   
                   &ArrayFields,
   
               )
           );
       ```
   
 * The intention is to add as many fields as needed from data created by my application.
 * Unable to find a way to do this, not if someone had a need and I could help.
 * Greetings and thanks in advance for any help.
 * [https://wordpress.org/plugins/admin-page-framework/](https://wordpress.org/plugins/admin-page-framework/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [miunosoft](https://wordpress.org/support/users/miunosoft/)
 * (@miunosoft)
 * [10 years, 12 months ago](https://wordpress.org/support/topic/dynamically-create-fields/#post-6167706)
 * Hi,
 * I guess you want to dynamically create _sortable_ fields. You need to define 
   numerically indexed array elements for the field definition array for sortable
   items.
 * In your case, maybe,
 *     ```
       $ArrayFields = array(
           'label'             => 'Apple',
           'default'           => 'a',
           array(
                  'label'             => 'Bananas',
                  'default'           => 'b',
           ),
           array(
                  'label'             => 'Cherry',
                  'default'           => 'c',
           ),
       );
   
       $this->addSettingFields(
           array(
               'field_id'          => 'sortable_labels',
               'type'              => 'hidden',
               'title'             => __( 'Sortale Labels', 'admin-page-framework-example' ),
               'sortable'          => true,
           ) + $ArrayFields
       );
       ```
   
 * Notice that the first sortable item (Apple) defined in the `$ArrayFields` array
   does not have a numeric index.
 * So just create your custom function to format the `$ArrayFields` array from your
   generated data.
 * Hope it helps.
 *  Thread Starter [Javier](https://wordpress.org/support/users/xarkitu/)
 * (@xarkitu)
 * [10 years, 12 months ago](https://wordpress.org/support/topic/dynamically-create-fields/#post-6167719)
 * Hey, this is great 🙂
 * This opens many doors for my coding.
 *     ```
       //My array
       $ArrayList = array(
          'a'	=> 'Apple',
          'b'  	=> 'Bananas',
          'c' 	=> 'Cherry,
       );
   
       //My default sorteable list
       foreach($ArrayList as $key => $lb){
          $def[] = $key;
       }
   
       //Get the fields...
       $first = true;
       foreach($this->getOption('sortable_labels', $def ) as $option) {
          if($first) {
             $MyFields = array(
                'label'         => $ArrayList[$option],
                'default'       => $option,
             );
             $first = false;
          } else {
             $MyFields[] =  array(
                'label'     => $ArrayList[$option],
                'default'   => $option,
             );
          }
       }
   
       //Publishing the field...
       $this->addSettingFields(
           array(
             'field_id'          => 'sortable_labels',
             'type'              => 'hidden',
             'title'             => __( 'Sortale Labels', 'admin-page-framework-example' ),
            'sortable'          => true,
           ) + $MyFields
       );
       ```
   
 * Miunosoft thank you very much for your support, I hope this helps anyone seeking
   to codify something.
 * best regards
 *  Plugin Author [miunosoft](https://wordpress.org/support/users/miunosoft/)
 * (@miunosoft)
 * [10 years, 12 months ago](https://wordpress.org/support/topic/dynamically-create-fields/#post-6167728)
 * Glad to hear you found a way! Good luck!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Dynamically create fields’ is closed to new replies.

 * ![](https://ps.w.org/admin-page-framework/assets/icon-256x256.png?rev=998199)
 * [Admin Page Framework](https://wordpress.org/plugins/admin-page-framework/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/admin-page-framework/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/admin-page-framework/)
 * [Active Topics](https://wordpress.org/support/plugin/admin-page-framework/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/admin-page-framework/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/admin-page-framework/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [miunosoft](https://wordpress.org/support/users/miunosoft/)
 * Last activity: [10 years, 12 months ago](https://wordpress.org/support/topic/dynamically-create-fields/#post-6167728)
 * Status: resolved