Custom Taxonomy Control for the Theme Customizer

Otto had a series of articles highlighting how to use the theme customizer in your WordPress themes instead of creating an options panel.

One of his articles covered making your own custom control and it inspired me to make a custom control for showing a certain taxonomy dropdown on the theme customizer. At first, I had just wanted this for the purpose of choosing from a dropdown of categories to use as featured posts in my theme. Then I thought it could really just as well be used for any taxonomy object. Behind the scenes, this control is using the wp_dropdown_categories function, which allows you to build a dropdown of categories (or any other registered taxonomy).

The code for the control really wasn’t bad. After actually implementing this and trying to use it, I found that my changes were not being saved. There is a data-customize-setting-link attribute that must be appended to your element. Below is the code from my functions.php file and the class being called by functions.php:

You may also like...

9 Responses

  1. Mohammad says:

    Thanks a lot for this. Helped a TON with getting it working after hours of chopping things together myself.

  2. Chris says:

    Thank… you… so… MUCH!

    I was trying to create a dropdown of pages based on the posts dropdown at… and your guide worked. I must have been missing something with other one.

    Thanks so much again, you really made my project possible.

  3. Chris says:

    Heh, 3 hours later I realize there is a dropdown-pages option. In my defense I had seen this on another site and tried it but it didn't work so I figured it was depreciated. Operator error because it works now. Oh well, I had fun.

  4. Dave Grffin says:

    Hi, brilliant bit code, thanks ever so much, saved me a load of headwork.

  5. Dave Grffin says:

    Hi Im now trying to get this to work making another section but using wp_dropdown_pages.

    It displays ok, but its not saving any changes. Any ideas ?

    • Eric Juden says:

      Hey Dave,

      You will probably have better luck recreating the wp_dropdown_pages function on your own. I'm not sure if you noticed in my wp_dropdown_cats() function above, but it has another attribute on the <code>select</code> for the link: <code>$this->get_link()</code>. You need that for it to actually save. I ran into the same thing when I first set this class up. And there is currently no way to add that in the wp_dropdown_pages() function.

  6. Chirag says:

    Eric – when I use this and hit Save & Publish on the Customize sidebar, it reverts back to the default_category (i.e., 1). Is there something missing to ensure the category selected (i.e, let's say 7=Events) is the category that stays selected after clicking Save&Publish button?? Other than that, this works brilliantly. Thx.

Leave a Reply

Your email address will not be published. Required fields are marked *