wppaste
WordPress

wp_dropdown_pages( array|string $args = '' ): string

Since
2.1.0, 4.2.0, 4.3.0
Source
wp-includes/post-template.php:1198
Retrieves or displays a list of pages as a dropdown (select list).

Compatibility

WordPress
since 4.3.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 page dropdown. See get_pages() for additional arguments.Default: ''
  • $depthintdefault: 0

    Maximum depth.
  • $child_ofintdefault: 0

    Page ID to retrieve child pages of.
  • $selectedint|stringdefault: 0

    Value of the option that should be selected.
  • $echobool|intdefault: 1

    Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents.
  • $namestringdefault: 'page_id'

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

    Value for the 'id' attribute of the select element.
  • $classstringdefault: none. Defaults to the value of $name

    Value for the 'class' attribute of the select element.
  • $show_option_nonestringdefault: empty (does not display)

    Text to display for showing no pages.
  • $show_option_no_changestringdefault: empty (does not display)

    Text to display for "no change" option.
  • $option_none_valuestringdefault: empty

    Value to use when no page is selected.
  • $value_fieldstringdefault: 'ID'

    Post field used to populate the 'value' attribute of the option elements. Accepts any valid post field.

Return value

string
HTML dropdown list of pages.

Performance profile

How much work a call to wp_dropdown_pages() 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_pages().

Scaling
Constant

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

Instructions
26–86

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

Plugin surface
1 hook

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

Called by
3

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

What it touches

  • querycontent queryget_pages()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 · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_dropdown_pages() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($pages)26–30wp_parse_args(), get_pages(), apply_filters()
!empty($pages) && empty($parsed_args) && !$parsed_args59–67wp_parse_args(), get_pages(), esc_attr(), esc_attr(), walk_page_dropdown_tree(), apply_filters()
!empty($pages)67–78wp_parse_args(), get_pages(), esc_attr(), esc_attr(), esc_attr(), walk_page_dropdown_tree(), apply_filters()
!empty($parsed_args) && !empty($pages) && $parsed_args78–86wp_parse_args(), get_pages(), esc_attr(), esc_attr(), esc_attr(), esc_attr(), walk_page_dropdown_tree(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 86 instructions, 26–86 executed per call, 6 branches. The work does not change between versions.

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 · 1

One hook fires while wp_dropdown_pages() runs, in this order:

  1. apply_filters( wp_dropdown_pages )filterline 1250 (+52 into the body)

    Filters the HTML output of a list of pages as a dropdown.

Uses · 5

Used by · 3

Source code

function wp_dropdown_pages( $args = '' ) {	$defaults = array(		'depth'                 => 0,		'child_of'              => 0,		'selected'              => 0,		'echo'                  => 1,		'name'                  => 'page_id',		'id'                    => '',		'class'                 => '',		'show_option_none'      => '',		'show_option_no_change' => '',		'option_none_value'     => '',		'value_field'           => 'ID',	); 	$parsed_args = wp_parse_args( $args, $defaults ); 	$pages  = get_pages( $parsed_args );	$output = '';	// Back-compat with old system where both id and name were based on $name argument.	if ( empty( $parsed_args['id'] ) ) {		$parsed_args['id'] = $parsed_args['name'];	} 	if ( ! empty( $pages ) ) {		$class = '';		if ( ! empty( $parsed_args['class'] ) ) {			$class = " class='" . esc_attr( $parsed_args['class'] ) . "'";		} 		$output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n";		if ( $parsed_args['show_option_no_change'] ) {			$output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</option>\n";		}		if ( $parsed_args['show_option_none'] ) {			$output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n";		}		$output .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args );		$output .= "</select>\n";	} 	/**	 * Filters the HTML output of a list of pages as a dropdown.	 *	 * @since 2.1.0	 * @since 4.4.0 `$parsed_args` and `$pages` added as arguments.	 *	 * @param string    $output      HTML output for dropdown list of pages.	 * @param array     $parsed_args The parsed arguments array. See wp_dropdown_pages()	 *                               for information on accepted arguments.	 * @param WP_Post[] $pages       Array of the page objects.	 */	$html = apply_filters( 'wp_dropdown_pages', $output, $parsed_args, $pages ); 	if ( $parsed_args['echo'] ) {		echo $html;	} 	return $html;}

Changelog

Introduced in 2.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.

4.3.0
The $class argument was added.from the docblock
4.2.0
The $value_field argument was added.from the docblock
2.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/post-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.