How Can I Create a List of Values to Be Iterated Through via WordPress Customization API?

I currently have a theme with OptionTree installed. It is used to create a list of locations which are then visible in a dropdown in the header of the page. The display code with OptionTree looks like this:

<form action="" id="formsel" method="post">
    <div class="locations">
        <?php $location = ot_get_option('location');?>
        <label for="country_id" class="no-display">Select Country</label>
        <select name="country_id" id="country_id" tabindex="1">
            <option value="">Location</option>
            <?php foreach($location as $listedlocation):?>
                   <option value="<?php echo $listedlocation[link];?>"><?php echo $listedlocation[title];?></option>
            <?php endforeach;?>

I'd like to convert this into something similar using the WordPress Customization API, but I am unsure how to accomplish this. I have successfully implemented the customization API where it takes a single value, but not where it needs to iterate over a number of values.


Assuming you have an array $locations available, you can use this code to generate a dropdown in the customizer. It's not that different from a single value.

$section_name = 'wpse213980_section_name'; // adapt to your naming system
$section_title = 'wpse213980_section_title'; // adapt to your naming system
$setting_name = 'wpse213980_setting_name'; // adapt to your naming system
$setting_field_type = 'select';
$setting_field_options = $locations;
$sanitize_callback = 'sanitize_text_field';

$wp_customize->add_setting($setting_name, array(
  'default'                 =>  '',
  'type'                    =>  'theme_mod',
  'capability'              =>  'edit_theme_options',
  'theme_supports'          =>  '',
  'transport'               =>  'refresh',
  'sanitize_callback'       =>  $sanitize_callback

$control_args = array(
    'section'                   =>  $section_name,
    'label'                     =>  $setting_title,
    'settings'                  =>  $setting_name,
    'priority'                  =>  10,
    'type'                      =>  $setting_field_type,
    'choices'                   =>  $setting_field_options,

$wp_customize->add_control( new WP_Customize_Control ($wp_customize, $setting_name, $control_args));

Props Otto.

Similar questions

Wordpress API Customization
we have an Educational websites built with Wordpress, which shows and previews it is content as categories and subcategories, depending on Educational Semester, Grade and subject. Recently, we needed to integrate this use content with another educational website using an API. the default API returns an out put of 9 posts only, with more not needed ...
What are the advantages and disadvantages of Option Tree over the Customization API?
I'm relatively new to WordPress theme development, and I had to create a theme with options available. I did a Google search to see what are the possibilities, and I read articles about such, and read about frameworks, and I came across Option Tree, which was recommended as a framework to develop with. What my question is, when I'm developing theme...
How to remove a settings section from the Theme Customization API preview pane?
I am currently working on a theme that uses the Wordpress Customization API and I need to remove some pre-existing sections from the customization preview pane. Is there somewhere like a global variable holding an array of the sections perhaps (in true Wordpress style) I can unset the navigation section in particular? Please see attached image for ...
Theme Customization API and child themes
I've recently developed a parent theme that makes heavy use of the Theme Customization API. Is there an easy way to port these settings when a user activates one of my child themes? Currently, the data previously saved in the Customize screen disappears when activating a child theme. Of course, once they reactivate the parent theme, the settings re...
Get background color for Live Preview with Theme Customization API?
This could very well be a stupid question, but I'm working on a WordPress theme (check the link to see what I'm talking about, then read on)... You'll see the Post Formats icons are surrounded by a white circle, which has a border color matching the custom background color. I know that Live Preview includes background color by default, but I'm wond...
Theme Customization API options on install
I'm working on a theme that use the WordPress Theme Customization API. Everything is working as it should, except on initial theme install. Some of my options need to be set straight away on theme install. After looking around it seems that setting the default arg in the add_setting option should set the option right away, but it's not. Here's a sh...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.