WordPress Theme Customizer – User Select Custom Control

Customize SSS V2 — WordPress

WordPress 3.5 is out and it’s great with the new media panel and all the fancy stuff, but its 3.4 theme customizer that is still the one to beat for me.  Finally we can begin to see a some sort of standard as far as the theme options pages, wow that’s is a relief. That said, I would love share a simple custom control I wrote for the theme customizer that allows you to create user select drop-down list, check out the code below or grab it on github.

if (class_exists('WP_Customize_Control')): 
class Selected_Users_Control extends WP_Customize_Control {

        public $type = 'option';
        public $query = array('orderby' => 'nicename');
        public $description = '';

        public function render_content() {
            $query = $this->query;
            $pgs = $this->user_array($query);
            ?>
            <label>
                <span  class="customize-control-title" ><?php echo esc_html($this->label); ?></span>
                <select <?php $this->link(); ?>>
                    <option></option>
            <?php foreach ($pgs as $key => $value): ?>
                        <option value="<?php echo $key ?>" <?php echo ($key == $this->value() ? 'selected' : '') ?>>
                        <?php echo $value; ?>
                        </option>
                        <?php endforeach; ?>
                </select>
                <span style="display: block"><?php echo esc_html($this->description) ?></span>
            </label>
            <?php
        }

        public function user_array($args = '') {
            /*
             * get list of pages and output an associate array with post_name and post-title
             * $pages['post_name'] = 'post_title';
             */

            $arrray = get_users($args);
            foreach ($arrray as $items) {
                $pgs["{$items->ID}"] = $items->user_nicename;
            }
            return $pgs;
        }

    }
endif;

It uses the get_users() function via a query (array) variable so you can customize the the user list, that’s pretty much it. Here’s a sample of it in action :-

   public function customize($customize) {
        $customize->add_section('bjc_theme_settings', array(
            'title' => 'Custom Settings',
            'priority' => 105,
            'description' => "Manage theme settings"
        ));

        $customize->add_setting('theme-admin', array(
            'default' => 'Default Theme Admin',
        ));

        $customize->add_control(new Selected_Users_Control($customize, 'theme-admin', array(
                    'label' => "Select Default ThemeAdmin",
                    'section' => 'bjc_theme_settings',
                    'settings' => 'theme-admin',
                    'description' => 'A Theme(s) admin user profile are used for themes socials links, contact info, about(us), etc',
                    'query' => array('role' => 'administrator')
                )));    }

Check out Otto’s great tutorial for more info on custom controls, post any questions of suggestions in the comments below.