On this page
wp_dropdown_pages( array|string $args = '' ): string
Retrieves or displays a list of pages as a dropdown (select list).
Description
See also
Parameters
$argsarray|string Optional-
Array or string of arguments to generate a page dropdown. See get_pages() for additional arguments.
depthintMaximum depth. Default 0.child_ofintPage ID to retrieve child pages of. Default 0.selectedint|stringValue of the option that should be selected. Default 0.echobool|intWhether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents. Default 1.namestringValue for the'name'attribute of the select element.
Default'page_id'.idstringValue for the'id'attribute of the select element.classstringValue for the'class'attribute of the select element. Default: none.
Defaults to the value of$name.show_option_nonestringText to display for showing no pages. Default empty (does not display).show_option_no_changestringText to display for "no change" option. Default empty (does not display).option_none_valuestringValue to use when no page is selected.value_fieldstringPost field used to populate the'value'attribute of the option elements. Accepts any valid post field. Default'ID'.
More Arguments from get_pages( ... $args )
Array or string of arguments to retrieve pages.
child_ofintPage ID to return child and grandchild pages of. Note: The value of$hierarchicalhas no bearing on whether$child_ofreturns hierarchical results. Default 0, or no restriction.sort_orderstringHow to sort retrieved pages. Accepts'ASC','DESC'. Default'ASC'.sort_columnstringWhat columns to sort pages by, comma-separated. Accepts'post_author','post_date','post_title','post_name','post_modified','menu_order','post_modified_gmt','post_parent','ID','rand','comment*count'.
'post*'can be omitted for any values that start with it.
Default'post_title'.hierarchicalboolWhether to return pages hierarchically. If false in conjunction with$child_ofalso being false, both arguments will be disregarded.
Default true.excludeint[]Array of page IDs to exclude.includeint[]Array of page IDs to include. Cannot be used with$child_of,$parent,$exclude,$meta_key,$meta_value, or$hierarchical.
meta_keystringOnly include pages with this meta key.meta_valuestringOnly include pages with this meta value. Requires$meta_key.
authorsstringA comma-separated list of author IDs.parentintPage ID to return direct children of. Default -1, or no restriction.exclude_treestring|int[]Comma-separated string or array of page IDs to exclude.
numberintThe number of pages to return. Default 0, or all pages.offsetintThe number of pages to skip before returning. Requires$number.
Default 0.post_typestringThe post type to query. Default'page'.post_statusstring|arrayA comma-separated list or array of post statuses to include.
Default'publish'.
Default:
''
Return
string HTML dropdown list of pages.
Source
File: wp-includes/post-template.php. View all references
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;
}
Hooks
- apply_filters( 'wp_dropdown_pages',
string $output ,array $parsed_args ,WP_Post[] $pages ) -
Filters the HTML output of a list of pages as a dropdown.
Related
Uses
| Uses | Description |
|---|---|
| walk_page_dropdown_tree() wp-includes/post-template.php | Retrieves HTML dropdown (select) content for page list. |
| get_pages() wp-includes/post.php | Retrieves an array of pages (or hierarchical post type items). |
| esc_attr() wp-includes/formatting.php | Escaping for HTML attributes. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| page_attributes_meta_box() wp-admin/includes/meta-boxes.php | Displays page attributes form fields. |
| WP_Posts_List_Table::inline_edit() wp-admin/includes/class-wp-posts-list-table.php | Outputs the hidden row displayed when inline editing |
| WP_Customize_Control::render_content() wp-includes/class-wp-customize-control.php | Render the control’s content. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_dropdown_pages