wp_dropdown_categories( array|string $args = '' ): string
- Since
- 2.1.0, 4.2.0, 4.6.0, 6.1.0
- Source
wp-includes/category-template.php:344
Description
The 'hierarchical' argument, which is disabled by default, will override the depth argument, unless it is true. When the argument is false, it will display all of the categories. When it is enabled it will use the value in the 'depth' argument.
Compatibility
- WordPress
- since 6.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsarray|stringoptional- Array or string of arguments to generate a categories drop-down element. See WP_Term_Query::__construct() for information on additional accepted arguments.Default:
''$show_option_allstringdefault: emptyText to display for showing all categories.$show_option_nonestringdefault: emptyText to display for showing no categories.$option_none_valuestringdefault: emptyValue to use when no category is selected.$orderbystringdefault: 'id' (term_id)Which column to use for ordering categories. See get_terms() for a list of accepted values.$pad_countsbooldefault: falseSee get_terms() for an argument description.$show_countbool|intdefault: 0Whether to include post counts. Accepts 0, 1, or their bool equivalents.$echobool|intdefault: 1Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents.$hierarchicalbool|intdefault: 0Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool equivalents.$depthintdefault: 0Maximum depth.$tab_indexintdefault: 0 (no tabindex)Tab index for the select element.$namestringdefault: 'cat'Value for the 'name' attribute of the select element.$idstringValue for the 'id' attribute of the select element. Defaults to the value of$name.$classstringdefault: 'postform'Value for the 'class' attribute of the select element.$selectedint|stringdefault: 0Value of the option that should be selected.$value_fieldstringdefault: 'term_id'Term field that should be used to populate the 'value' attribute of the option elements. Accepts any valid term field: 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count'.$taxonomystring|arraydefault: 'category'Name of the taxonomy or taxonomies to retrieve.$hide_if_emptybooldefault: false (create select element even if no categories are found)True to skip generating markup if no categories are found.$requiredbooldefault: falseWhether the<select>element should have the HTML5 'required' attribute.$walkerWalkerdefault: empty which results in a Walker_CategoryDropdown instance being usedWalker object to use to build the output.$aria_describedbystringdefault: empty stringThe 'id' of an element that contains descriptive text for the select.
Return value
string- HTML dropdown list of categories.
Performance profile
How much work a call to wp_dropdown_categories() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 74–180
- Plugin surface
- 2 hooks
- Called by
- 7
Reaches the database via get_terms().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 225.
Third-party callbacks on 'list_cats', 'wp_dropdown_cats' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_terms()called directly - hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 20 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_dropdown_categories() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_category() && !isset($args) && isset($parsed_args) && !$parsed_args && empty($categories) | 74–94 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && empty($categories) | 81–101 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !$parsed_args && !empty($categories) | 87–108 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($categories) | 94–115 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !$parsed_args && empty($categories) && !empty($parsed_args) | 95–111 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && empty($categories) && !empty($parsed_args) | 102–118 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($categories) | 107–129 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !$parsed_args && !empty($parsed_args) | 108–125 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($categories) | 114–136 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($categories) | 114–135 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($parsed_args) | 115–132 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($categories) | 121–142 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
8 further outcomes, up to 180 instructions
!is_category() && !isset($args) && isset($parsed_args) && !empty($parsed_args) | 128–146 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($categories) | 134–156 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($parsed_args) | 135–153 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($parsed_args) | 135–152 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($categories) | 141–163 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($parsed_args) | 142–159 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($parsed_args) | 155–173 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
!is_category() && !isset($args) && isset($parsed_args) && !empty($parsed_args) | 162–180 | is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), esc_attr(), apply_filters(), apply_filters(), selected(), esc_attr(), walk_category_dropdown_tree(), apply_filters() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 223 | 74–180 | 23 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 225 | 74–180 | 23 | |
| 8.4 | 225 | 74–180 | 23 | |
| 8.3 | 225 | 74–180 | 23 | |
| 8.2 | 225 | 74–180 | 23 | |
| 8.1 | 225 | 74–180 | 23 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 226 | 74–181 | 23 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 4
4 hooks fire while wp_dropdown_categories() runs, in this order:
- apply_filters( list_cats )filterline 437 (+93 into the body)
Filters a taxonomy drop-down display element.
- apply_filters( list_cats )filterline 446 (+102 into the body)
Filters a taxonomy drop-down display element.
- apply_filters( list_cats )filterline 454 (+110 into the body)
Filters a taxonomy drop-down display element.
- apply_filters( wp_dropdown_cats )filterline 479 (+135 into the body)
Filters the taxonomy drop-down output.
Uses · 10
- is_category()Determines whether the query is for an existing category archive page.
- get_query_var()Retrieves the value of a query variable in the WP_Query class.
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
- get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
- esc_attr()Escaping for HTML attributes.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- selected()Outputs the HTML selected attribute.
- walk_category_dropdown_tree()Retrieves HTML dropdown (select) content for category list.
Used by · 7
- WP_Links_List_Table::extra_tablenav()
- WP_Posts_List_Table::categories_dropdown()Displays a categories drop-down for filtering on the Posts list table.
- WP_Widget_Categories::widget()Outputs the content for the current Categories widget instance.
- _wp_ajax_add_hierarchical_term()Handles adding a hierarchical term via AJAX.
- dropdown_cats()Deprecated method for generating a drop-down of categories.
- post_categories_meta_box()Displays post categories form fields.
- render_block_core_categories()Renders the `core/categories` block on server.
Source code
function wp_dropdown_categories( $args = '' ) { $defaults = array( 'show_option_all' => '', 'show_option_none' => '', 'orderby' => 'id', 'order' => 'ASC', 'show_count' => 0, 'hide_empty' => 1, 'child_of' => 0, 'exclude' => '', 'echo' => 1, 'selected' => 0, 'hierarchical' => 0, 'name' => 'cat', 'id' => '', 'class' => 'postform', 'depth' => 0, 'tab_index' => 0, 'taxonomy' => 'category', 'hide_if_empty' => false, 'option_none_value' => -1, 'value_field' => 'term_id', 'required' => false, 'aria_describedby' => '', ); $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; // Back compat. if ( isset( $args['type'] ) && 'link' === $args['type'] ) { _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( /* translators: 1: "type => link", 2: "taxonomy => link_category" */ __( '%1$s is deprecated. Use %2$s instead.' ), '<code>type => link</code>', '<code>taxonomy => link_category</code>' ) ); $args['taxonomy'] = 'link_category'; } // Parse incoming $args into an array and merge it with $defaults. $parsed_args = wp_parse_args( $args, $defaults ); $option_none_value = $parsed_args['option_none_value']; if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) { $parsed_args['pad_counts'] = true; } $tab_index = $parsed_args['tab_index']; $tab_index_attribute = ''; if ( (int) $tab_index > 0 ) { $tab_index_attribute = " tabindex=\"$tab_index\""; } // Avoid clashes with the 'name' param of get_terms(). $get_terms_args = $parsed_args; unset( $get_terms_args['name'] ); $categories = get_terms( $get_terms_args ); $name = esc_attr( $parsed_args['name'] ); $class = esc_attr( $parsed_args['class'] ); $id = $parsed_args['id'] ? esc_attr( $parsed_args['id'] ) : $name; $required = $parsed_args['required'] ? 'required' : ''; $aria_describedby_attribute = $parsed_args['aria_describedby'] ? ' aria-describedby="' . esc_attr( $parsed_args['aria_describedby'] ) . '"' : ''; if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) { $output = "<select $required name='$name' id='$id' class='$class'$tab_index_attribute$aria_describedby_attribute>\n"; } else { $output = ''; } if ( empty( $categories ) && ! $parsed_args['hide_if_empty'] && ! empty( $parsed_args['show_option_none'] ) ) { /** * Filters a taxonomy drop-down display element.Changelog
Introduced in 6.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
aria_describedby argument.from the docblockrequired argument.from the docblockvalue_field argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/category-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.