2

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' );
4
  • 1
    Could you elaborate a bit? Where/how are you running your code? Do you mean adding term of taxonomy, or new taxonomy itself?
    – Rarst
    Commented Jun 14, 2014 at 16:30
  • I don't know what are trying to achive??? I have worked on 200+ wordpress projects. And never got this type of situation... Am i missing something???
    – Shail
    Commented Jun 17, 2014 at 4:55
  • Yeah, sorry, I'm new to Wordpress development, so not very good with the terminology, but I figured it out. Commented Jun 17, 2014 at 17:15
  • If the answer was helpful to you, then consider accepting it. See »What should I do when someone answers my question?« and/or »Why is voting important?«, more information about the WordPress Development model is available at the help center. Commented Apr 30, 2015 at 15:31

2 Answers 2

3
+25

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.

1

Short answer: Both functions can be hooked to customize_register, and they will work fine. The customize_register hook only works on the customize page, but I need the register_taxonomy function on both pages. So, in order to use the register_taxonomy function on a front-end page call, I also called it on the init hook.


"So that problem might have different reasons"

ialocin, You're right...

Without getting too deep, I was expecting the customize_register hook to call on every page call (because that's what the documentation says), but it doesn't, and that threw my troublshooting in completely the wrong direction.

Thanks for the help!

3
  • My pleasure! Not sure where you got the impression from that the hook does load on every page load, doesn't say so on the customize_register codex page. On the other hand it explicitly states »Use the init action to call this function.« on the register_taxonomy codex page. Anyway, problem solved. Commented Jun 17, 2014 at 11:16
  • 1
    codex.wordpress.org/Plugin_API/Action_Reference says "customize_register Runs on every request, allowing developers to register new theme options and controls for use with the Theme Customization API" Commented Jun 17, 2014 at 17:11
  • Ok, I see it now, looked at singular page for customize_register, and BTW it is true, because the customize_register action is hooked to wp_loaded. Commented Jun 17, 2014 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.