Skip to content

Instantly share code, notes, and snippets.

@stephenh1988
Created June 9, 2012 20:40
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save stephenh1988/2902509 to your computer and use it in GitHub Desktop.
Save stephenh1988/2902509 to your computer and use it in GitHub Desktop.
A walker class to use that extends wp_dropdown_categories and allows it to use the term's slug as a value rather than ID
<?php
/*
* A walker class to use that extends wp_dropdown_categories and allows it to use the term's slug as a value rather than ID.
*
* See http://core.trac.wordpress.org/ticket/13258
*
* Usage, as normal:
* wp_dropdown_categories($args);
*
* But specify the custom walker class, and (optionally) a 'id' or 'slug' for the 'value' parameter:
* $args=array('walker'=> new SH_Walker_TaxonomyDropdown(), 'value'=>'slug', .... );
* wp_dropdown_categories($args);
*
* If the 'value' parameter is not set it will use term ID for categories, and the term's slug for other taxonomies in the value attribute of the term's <option>.
*/
class SH_Walker_TaxonomyDropdown extends Walker_CategoryDropdown{
function start_el(&$output, $category, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
$cat_name = apply_filters('list_cats', $category->name, $category);
if( !isset($args['value']) ){
$args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' );
}
$value = ($args['value']=='slug' ? $category->slug : $category->term_id );
$output .= "\t<option class=\"level-$depth\" value=\"".$value."\"";
if ( $value === (string) $args['selected'] ){
$output .= ' selected="selected"';
}
$output .= '>';
$output .= $pad.$cat_name;
if ( $args['show_count'] )
$output .= '&nbsp;&nbsp;('. $category->count .')';
$output .= "</option>\n";
}
}
@stephenh1988
Copy link
Author

@pell
Copy link

pell commented Aug 23, 2012

Wonderful. (: This helped me out a lot, thank you.

@semitono
Copy link

semitono commented Jul 4, 2013

Thank you, Stephen. You save my time so much!

@dashaluna
Copy link

Thank you, Stephen! Helped me a lot with my current project.

@dashaluna
Copy link

Hello Stephen, apologies if this is not the right place to ask.. I was wondering how to better remove 'value="0"' on $show_option_all argument? I have a GET search form and if $show_option_all option is selected, then I'm getting select_name=0 in my URL and I don't want it appear at all if $show_option_all option is selected. Any advice what to do?

Many thanks!

@hchouhan
Copy link

Hi Stephen,
I had been using this for few projects. But for a new one, I get below error:

Strict Standards: Declaration of SH_Walker_TaxonomyDropdown::start_el() should be compatible with Walker_CategoryDropdown::start_el(&$output, $category, $depth = 0, $args = Array, $id = 0) in

@amallory
Copy link

hchouhan: if you modify the function call to be start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { That will fix it. SH_Walker_TaxonomyDropdown has different arguments from what Walker_CategoryDropdown has defined, this will make them as the same. Note the defaults on '''$depth''', and '''$id'''

@eddiemoya
Copy link

Thanks for this. Saved me a bunch of time and effort digging around and looking for the original walker and modifying it.

This is going to be included in one of my public plugins. "Media Categories".
https://github.com/eddiemoya/Media-Categories

@andreuka
Copy link

thank for sharing!

@sureshHARDIYA
Copy link

Wonderful, saved my time. Than you for sharing the code.

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