Skip to content

Instantly share code, notes, and snippets.

@ericjuden
Last active September 3, 2020 13:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ericjuden/4656209 to your computer and use it in GitHub Desktop.
Save ericjuden/4656209 to your computer and use it in GitHub Desktop.
Taxonomy Dropdown Control for WordPress Theme Customizer
<?php
add_action('customize_register', 'my_customize_register');
function my_customize_register($wp_customize){
require_once(TEMPLATEPATH . '/class/wp_customizer_taxonomy_dropdown.php');
$wp_customize->add_section('my_theme_blog_featured_categories', array(
'title' => __('Blog: Featured Categories'),
'priority' => 36,
));
$wp_customize->add_setting('featured_category_1', array(
'default' => get_option('default_category', ''),
));
$wp_customize->add_control( new Taxonomy_Dropdown_Customize_Control($wp_customize, 'featured_category_1', array(
'label' => __('Featured Area 1'),
'section' => 'my_theme_blog_featured_categories',
'settings' => 'featured_category_1',
'args' => array(), // arguments for wp_dropdown_categories function...optional. array('taxonomy' => 'my_taxonomy')
)));
return $wp_customize;
}
?>
<?php
class Taxonomy_Dropdown_Customize_Control extends WP_Customize_Control {
public $type = 'taxonomy_dropdown';
var $defaults = array();
public $args = array();
public function render_content(){
// Call wp_dropdown_cats to ad data-customize-setting-link to select tag
add_action('wp_dropdown_cats', array($this, 'wp_dropdown_cats'));
// Set some defaults for our control
$this->defaults = array(
'show_option_none' => __('None'),
'orderby' => 'name',
'hide_empty' => 0,
'id' => $this->id,
'selected' => $this->value(),
);
// Parse defaults against what the user submitted
$r = wp_parse_args($this->args, $this->defaults);
?>
<label><span class="customize-control-title"><?php echo esc_html($this->label); ?></span></label>
<?php
// Generate our select box
wp_dropdown_categories($r);
}
function wp_dropdown_cats($output){
// Search for <select and replace it with <select data-customize=setting-link="my_control_id"
$output = str_replace('<select', '<select ' . $this->get_link(), $output);
return $output;
}
}
?>
@lkraav
Copy link

lkraav commented Feb 8, 2013

Thanks for publishing (y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment