wppaste
WordPress

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
Displays or retrieves the HTML dropdown list of categories.

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: empty

    Text to display for showing all categories.
  • $show_option_nonestringdefault: empty

    Text to display for showing no categories.
  • $option_none_valuestringdefault: empty

    Value 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: false

    See get_terms() for an argument description.
  • $show_countbool|intdefault: 0

    Whether to include post counts. Accepts 0, 1, or their bool equivalents.
  • $echobool|intdefault: 1

    Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents.
  • $hierarchicalbool|intdefault: 0

    Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool equivalents.
  • $depthintdefault: 0

    Maximum depth.
  • $tab_indexintdefault: 0 (no tabindex)

    Tab index for the select element.
  • $namestringdefault: 'cat'

    Value for the 'name' attribute of the select element.
  • $idstring

    Value 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: 0

    Value 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: false

    Whether the <select> element should have the HTML5 'required' attribute.
  • $walkerWalkerdefault: empty which results in a Walker_CategoryDropdown instance being used

    Walker object to use to build the output.
  • $aria_describedbystringdefault: empty string

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

Reaches the database via get_terms().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
74–180

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 225.

Plugin surface
2 hooks

Third-party callbacks on 'list_cats', 'wp_dropdown_cats' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_terms()called directly
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!is_category() && !isset($args) && isset($parsed_args) && !$parsed_args && empty($categories)74–94is_category(), wp_parse_args(), get_terms(), esc_attr(), esc_attr(), apply_filters()
!is_category() && !isset($args) && isset($parsed_args) && empty($categories)81–101is_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–108is_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–115is_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–111is_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–118is_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–129is_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–125is_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–136is_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–135is_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–132is_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–142is_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–146is_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–156is_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–153is_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–152is_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–163is_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–159is_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–173is_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–180is_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

PHPCompiledExecutedBranchesNotes
8.6-dev22374–180232 fewer instructions than PHP 8.5
8.522574–18023
8.422574–18023
8.322574–18023
8.222574–18023
8.122574–180231 fewer instruction than PHP 7.4
7.422674–18123

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:

  1. apply_filters( list_cats )filterline 437 (+93 into the body)

    Filters a taxonomy drop-down display element.

  2. apply_filters( list_cats )filterline 446 (+102 into the body)

    Filters a taxonomy drop-down display element.

  3. apply_filters( list_cats )filterline 454 (+110 into the body)

    Filters a taxonomy drop-down display element.

  4. apply_filters( wp_dropdown_cats )filterline 479 (+135 into the body)

    Filters the taxonomy drop-down output.

Uses · 10

Used by · 7

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.1.0
Introduced the aria_describedby argument.from the docblock
4.6.0
Introduced the required argument.from the docblock
4.2.0
Introduced the value_field argument.from the docblock
2.1.0
Introduced.from the docblock

About 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.