wp_dropdown_pages( array|string $args = '' ): string
- Since
- 2.1.0, 4.2.0, 4.3.0
- Source
wp-includes/post-template.php:1201
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: 0Maximum depth.$child_ofintdefault: 0Page ID to retrieve child pages of.$selectedint|stringdefault: 0Value of the option that should be selected.$echobool|intdefault: 1Whether 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.$idstringValue for the 'id' attribute of the select element.$classstringdefault: none. Defaults to the value of $nameValue 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: emptyValue 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
- Scaling
- Constant
- Instructions
- 26–86
- Plugin surface
- 1 hook
- Called by
- 3
Reaches the database via get_pages().
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 86.
Third-party callbacks on 'wp_dropdown_pages' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_pages()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 · 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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($pages) | 26–30 | wp_parse_args(), get_pages(), apply_filters() |
!empty($pages) && empty($parsed_args) && !$parsed_args | 59–67 | wp_parse_args(), get_pages(), esc_attr(), esc_attr(), walk_page_dropdown_tree(), apply_filters() |
!empty($pages) | 67–78 | wp_parse_args(), get_pages(), esc_attr(), esc_attr(), esc_attr(), walk_page_dropdown_tree(), apply_filters() |
!empty($parsed_args) && !empty($pages) && $parsed_args | 78–86 | wp_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:
- apply_filters( wp_dropdown_pages )filterline 1253 (+52 into the body)
Filters the HTML output of a list of pages as a dropdown.
Uses · 5
- wp_parse_args()Merges user defined arguments into defaults array.
- get_pages()Retrieves an array of pages (or hierarchical post type items).
- esc_attr()Escaping for HTML attributes.
- walk_page_dropdown_tree()Retrieves HTML dropdown (select) content for page list.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 3
- WP_Customize_Control::render_content()Renders the control's content.
- WP_Posts_List_Table::inline_edit()Outputs the hidden row displayed when inline editing
- page_attributes_meta_box()Displays page attributes form fields.
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.
Signature, return type and hooks compared across 5 parsed releases.
$class argument was added.from the docblock$value_field argument was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.