Last active
September 3, 2020 13:07
-
-
Save ericjuden/4656209 to your computer and use it in GitHub Desktop.
Taxonomy Dropdown Control for WordPress Theme Customizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for publishing (y)