wppaste
WordPress

wp_page_menu( array|string $args = array() ): void|string

Since
2.7.0, 4.4.0, 4.7.0
Source
wp-includes/post-template.php:1426
Displays or retrieves a list of pages with an optional home link.

Description

The arguments are listed below and part of the arguments are for wp_list_pages() function.
Check that function for more info on those arguments.

Compatibility

WordPress
since 4.7.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 menu. See wp_list_pages() for additional arguments.Default: array()
  • $sort_columnstringdefault: 'menu_order, post_title'

    How to sort the list of pages. Accepts post column names.
  • $menu_idstringdefault: is empty string

    ID for the div containing the page list.
  • $menu_classstringdefault: 'menu'

    Class to use for the element containing the page list.
  • $containerstringdefault: 'div'

    Element to use for the element containing the page list.
  • $echobooldefault: true

    Whether to echo the list or return it. Accepts true (echo) or false (return).
  • $show_homeint|bool|string

    Whether to display the link to the home page. Can just enter the text you'd like shown for the home link. 1|true defaults to 'Home'.
  • $link_beforestringdefault: empty

    The HTML or text to prepend to $show_home text.
  • $link_afterstringdefault: empty

    The HTML or text to append to $show_home text.
  • $beforestringdefault: is ''

    The HTML or text to prepend to the menu.
  • $afterstringdefault: is ''

    The HTML or text to append to the menu.
  • $item_spacingstringdefault: 'discard'

    Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'.
  • $walkerWalkerdefault: empty which results in a Walker_Page instance being used

    Walker instance to use for listing pages.

Return value

void|string
Void if 'echo' argument is true, HTML menu if 'echo' is false.

Performance profile

How much work a call to wp_page_menu() 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
65–154

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

Plugin surface
2 hooks

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

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • cacheobject cachewp_cache_get()one call below wp_page_menu()
  • serializeserialisationmaybe_unserialize()one call below wp_page_menu()
  • querycontent queryget_pages()one call below wp_page_menu()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 27 distinct outcomes

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

WhenInstructionsCalls it makes
empty($args)65–85wp_parse_args(), apply_filters(), wp_list_pages(), sanitize_text_field(), apply_filters()
always73–93wp_parse_args(), apply_filters(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
always81–101wp_parse_args(), apply_filters(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
!is_front_page()99–124wp_parse_args(), apply_filters(), __(), is_front_page(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
!is_front_page()101–120wp_parse_args(), apply_filters(), is_front_page(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
is_front_page()102–128wp_parse_args(), apply_filters(), __(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
is_front_page()104–124wp_parse_args(), apply_filters(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
!is_front_page()107–132wp_parse_args(), apply_filters(), __(), is_front_page(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
!is_front_page()108–134wp_parse_args(), apply_filters(), __(), is_front_page(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
!is_front_page()109–128wp_parse_args(), apply_filters(), is_front_page(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
!is_front_page()110–130wp_parse_args(), apply_filters(), is_front_page(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
is_front_page()110–136wp_parse_args(), apply_filters(), __(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
15 further outcomes, up to 150 instructions
is_front_page()111–138wp_parse_args(), apply_filters(), __(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
is_front_page()112–132wp_parse_args(), apply_filters(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
is_front_page()113–134wp_parse_args(), apply_filters(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), apply_filters()
!empty($args) && !is_front_page()115–140wp_parse_args(), apply_filters(), __(), is_front_page(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
!is_front_page()116–142wp_parse_args(), apply_filters(), __(), is_front_page(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
!empty($args) && !is_front_page()117–136wp_parse_args(), apply_filters(), is_front_page(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
!is_front_page()118–138wp_parse_args(), apply_filters(), is_front_page(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
!empty($args) && is_front_page()118–144wp_parse_args(), apply_filters(), __(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
is_front_page()119–146wp_parse_args(), apply_filters(), __(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
!empty($args) && is_front_page()120–140wp_parse_args(), apply_filters(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
is_front_page()121–142wp_parse_args(), apply_filters(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), apply_filters()
!empty($args) && !is_front_page()124–150wp_parse_args(), apply_filters(), __(), is_front_page(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
!empty($args) && !is_front_page()126–146wp_parse_args(), apply_filters(), is_front_page(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
!empty($args) && is_front_page()127–154wp_parse_args(), apply_filters(), __(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), apply_filters()
!empty($args) && is_front_page()129–150wp_parse_args(), apply_filters(), is_front_page(), is_paged(), home_url(), esc_url(), get_option(), get_option(), wp_list_pages(), sanitize_text_field(), esc_attr(), esc_attr(), 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

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 163 instructions, 65–154 executed per call, 18 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 · 2

2 hooks fire while wp_page_menu() runs, in this order:

  1. apply_filters( wp_page_menu_args )filterline 1465 (+39 into the body)

    Filters the arguments used to generate a page-based menu.

  2. apply_filters( wp_page_menu )filterline 1540 (+114 into the body)

    Filters the HTML output of a page-based menu.

Uses · 11

  • wp_parse_args()Merges user defined arguments into defaults array.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • __()Retrieves the translation of $text.
  • is_front_page()Determines whether the query is for the front page of the site.
  • is_paged()Determines whether the query is for a paged result and not for the first page.
  • esc_url()Checks and cleans a URL.
  • home_url()Retrieves the URL for the current site where the front end is accessible.
  • get_option()Retrieves an option value based on an option name.
  • wp_list_pages()Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format.
  • sanitize_text_field()Sanitizes a string from user input or from the database.
  • esc_attr()Escaping for HTML attributes.

Source code

function wp_page_menu( $args = array() ) {	$defaults = array(		'sort_column'  => 'menu_order, post_title',		'menu_id'      => '',		'menu_class'   => 'menu',		'container'    => 'div',		'echo'         => true,		'link_before'  => '',		'link_after'   => '',		'before'       => '<ul>',		'after'        => '</ul>',		'item_spacing' => 'discard',		'walker'       => '',	);	$args     = wp_parse_args( $args, $defaults ); 	if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {		// Invalid value, fall back to default.		$args['item_spacing'] = $defaults['item_spacing'];	} 	if ( 'preserve' === $args['item_spacing'] ) {		$t = "\t";		$n = "\n";	} else {		$t = '';		$n = '';	} 	/**	 * Filters the arguments used to generate a page-based menu.	 *	 * @since 2.7.0	 *	 * @see wp_page_menu()	 *	 * @param array $args An array of page menu arguments. See wp_page_menu()	 *                    for information on accepted arguments.	 */	$args = apply_filters( 'wp_page_menu_args', $args ); 	$menu = ''; 	$list_args = $args; 	// Show Home in the menu.	if ( ! empty( $args['show_home'] ) ) {		if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] ) {			$text = __( 'Home' );		} else {			$text = $args['show_home'];		}		$class = '';		if ( is_front_page() && ! is_paged() ) {			$class = 'class="current_page_item"';		}		$menu .= '<li ' . $class . '><a href="' . esc_url( home_url( '/' ) ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';		// If the front page is a page, add it to the exclude list.		if ( 'page' === get_option( 'show_on_front' ) ) {			if ( ! empty( $list_args['exclude'] ) ) {				$list_args['exclude'] .= ',';			} else {				$list_args['exclude'] = '';			}			$list_args['exclude'] .= get_option( 'page_on_front' );		}	} 	$list_args['echo']     = false;	$list_args['title_li'] = '';	$menu                 .= wp_list_pages( $list_args ); 	$container = sanitize_text_field( $args['container'] ); 	// Fallback in case `wp_nav_menu()` was called without a container.	if ( empty( $container ) ) {		$container = 'div';	} 	if ( $menu ) {

Changelog

Introduced in 2.7.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.7.0
Added the item_spacing argument.from the docblock
4.4.0
Added menu_id, container, before, after, and walker arguments.from the docblock
2.7.0
Introduced.from the docblock

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