How do I add customize_register action AFTER adding a custom taxonomy

customize_register only works with the customize_register hook, and regardless of what hook I use to add the custom taxonomy, it always happens after customize_register.

(I want to loop through the list of terms from my custom taxonomy to create categories and custom options for each term)

EDIT:

This is the watered-down code in question of my template functions file:

register_taxonomy( 'brand', $object_types, $args );
add_action( 'muplugins_loaded', 'brand', 0 );
// ????? What Hook can I use here^ that will run before the 'customize_register' hook ????? //

function iartz_customize_register( $wp_customize ) {
    $brands = get_terms('brand');
    foreach($brands AS $brand){
        //Add color options for each term of taxtonomy 'brand'
    }
}
add_action( 'customize_register', 'iartz_customize_register' );

2 Answers
2

AFAIK it shouldn’t be a problem to get taxonomy data at the customizer_register hook state. So that problem might have different reasons, but they aren’t apparent from your question. Besides I’m not sure what you are trying to achieve, so you might want to fill the information gaps according to @Rarst’s questions. Aside from that there are multiple methods out there to – for example – add a taxonomy dropdown to the customizer – I picked one and linked another one, see below.


Disclaimer:
This isn’t my code, I add it here for completeness reasons, links to the source are added. Haven’t tried it myself, but as far as I can tell there isn’t anything wrong with it and it is working for people.


Code for taxonomy dropdown for theme customizer

→ Source at Gist
→ according blog article by Eric Juden

This goes into the functions.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 goes into a file called wp_customizer_taxonomy_dropdown.php located in a folder, located relatively to the functions.php, called class:

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;
    }
}

This should with the additional information linked be pretty much self-explaining.

Additional similar approach

→ WordPress Theme Customizer Custom Controls by @bueltge;
→ Link to source of taxonomy dropdown example at Gist;
→ His answer on here regarding this topic;

→ There are a couple of tutorials, articles and such available too, you’ll find them easily yourself if you need to.

Leave a Comment