Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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";
}
}
@foxinni

This comment has been minimized.

Show comment Hide comment
@foxinni

foxinni Jun 13, 2012

Great work! Perfect.

Just a note. You need to keep the $args array one level deep. I'm using this walker snippet to filter (custom) posts in the edit screen on a taxonomy via the dropdown and 'restrict_manage_posts' action.

My active code here: https://gist.github.com/2922611

Thanks,
M

foxinni commented Jun 13, 2012

Great work! Perfect.

Just a note. You need to keep the $args array one level deep. I'm using this walker snippet to filter (custom) posts in the edit screen on a taxonomy via the dropdown and 'restrict_manage_posts' action.

My active code here: https://gist.github.com/2922611

Thanks,
M

@stephenh1988

This comment has been minimized.

Show comment Hide comment
@stephenh1988

stephenh1988 Jun 13, 2012

Thanks! I'm not sure what you mean about keeping the $args array one level deep...?

Owner

stephenh1988 commented Jun 13, 2012

Thanks! I'm not sure what you mean about keeping the $args array one level deep...?

@foxinni

This comment has been minimized.

Show comment Hide comment
@foxinni

foxinni Jun 13, 2012

No problem!

What I meant by 'one level deep' is, that in your commented usage description you state: $args=array(array('walker'=> new SH_Walker_TaxonomyDropdown(), 'value'=>'slug', .... ) and that didn't work for me.

Should just be $args=array('walker'=> new SH_Walker_TaxonomyDropdown(), 'value'=>'slug', .... ); which did work for me. :)

M

P.S. The codex does not mention you can use a custom walker (http://codex.wordpress.org/Function_Reference/wp_dropdown_categories) so I would never have though of it to fix this issue. Thanks again.

foxinni commented Jun 13, 2012

No problem!

What I meant by 'one level deep' is, that in your commented usage description you state: $args=array(array('walker'=> new SH_Walker_TaxonomyDropdown(), 'value'=>'slug', .... ) and that didn't work for me.

Should just be $args=array('walker'=> new SH_Walker_TaxonomyDropdown(), 'value'=>'slug', .... ); which did work for me. :)

M

P.S. The codex does not mention you can use a custom walker (http://codex.wordpress.org/Function_Reference/wp_dropdown_categories) so I would never have though of it to fix this issue. Thanks again.

@stephenh1988

This comment has been minimized.

Show comment Hide comment
@stephenh1988

stephenh1988 Jun 13, 2012

Ah, that was a type. Fixed now :). Thanks! Yeah - I might edit the Codex to include that. In fact I think all the functions that rely on a Walker class allow you to over-ride it with a custom one.

Owner

stephenh1988 commented Jun 13, 2012

Ah, that was a type. Fixed now :). Thanks! Yeah - I might edit the Codex to include that. In fact I think all the functions that rely on a Walker class allow you to over-ride it with a custom one.

@andersjohansson

This comment has been minimized.

Show comment Hide comment
@andersjohansson

andersjohansson Jul 11, 2012

The name parameter for the select element needs to be set correctly as well. So getting it right requires one to pass the correct taxonomy and name arguments (as well as the new walker for now) to wp_dropdown_categories() e.g.: 'taxonomy'=>'post_tag', 'name' => 'tag'. Shouldn't this be handled automatically in some depending on the chosen taxonomy.

The name parameter for the select element needs to be set correctly as well. So getting it right requires one to pass the correct taxonomy and name arguments (as well as the new walker for now) to wp_dropdown_categories() e.g.: 'taxonomy'=>'post_tag', 'name' => 'tag'. Shouldn't this be handled automatically in some depending on the chosen taxonomy.

@stephenh1988

This comment has been minimized.

Show comment Hide comment
@stephenh1988

stephenh1988 Jul 12, 2012

That would be a useful addition as well, but would require editing wp_dropdown_categories (see source https://github.com/WordPress/WordPress/blob/3.4.1/wp-includes/category-template.php#L338). This was just posted as a work-around for an existing trac ticket, and currently the only way to override the select name is to provide it with one. I don't think that will ever be 'fixed' since it won't be backwards compatible.

Owner

stephenh1988 commented Jul 12, 2012

That would be a useful addition as well, but would require editing wp_dropdown_categories (see source https://github.com/WordPress/WordPress/blob/3.4.1/wp-includes/category-template.php#L338). This was just posted as a work-around for an existing trac ticket, and currently the only way to override the select name is to provide it with one. I don't think that will ever be 'fixed' since it won't be backwards compatible.

@stephenh1988

This comment has been minimized.

Show comment Hide comment
@pell

This comment has been minimized.

Show comment Hide comment
@pell

pell Aug 23, 2012

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

pell commented Aug 23, 2012

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

@alexastro

This comment has been minimized.

Show comment Hide comment
@alexastro

alexastro Jul 4, 2013

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

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

@dashaluna

This comment has been minimized.

Show comment Hide comment
@dashaluna

dashaluna Jan 17, 2014

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

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

@dashaluna

This comment has been minimized.

Show comment Hide comment
@dashaluna

dashaluna Jan 17, 2014

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!

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

This comment has been minimized.

Show comment Hide comment
@hchouhan

hchouhan Mar 14, 2014

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

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

This comment has been minimized.

Show comment Hide comment
@amallory

amallory Mar 26, 2014

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'''

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

This comment has been minimized.

Show comment Hide comment
@eddiemoya

eddiemoya Mar 27, 2014

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

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

This comment has been minimized.

Show comment Hide comment
@andreuka

andreuka May 21, 2014

thank for sharing!

thank for sharing!

@sureshHARDIYA

This comment has been minimized.

Show comment Hide comment
@sureshHARDIYA

sureshHARDIYA Aug 26, 2015

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

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