wppaste
WordPress

WP_Nav_Menu_Widget::widget( array $args, array $instance )

Since
3.0.0
Source
wp-includes/widgets/class-wp-nav-menu-widget.php:42
Outputs the content for the current Navigation Menu widget instance.

Compatibility

WordPress
since 3.0.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
Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
$instancearray
Settings for the current Navigation Menu widget instance.

Performance profile

How much work a call to WP_Nav_Menu_Widget::widget() 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_term().

Scaling
Constant

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

Instructions
8–83

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

Plugin surface
3 hooks

Third-party callbacks on 'widget_title', 'navigation_widgets_format', 'widget_nav_menu_args' 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
  • querycontent queryget_term()one call below WP_Nav_Menu_Widget::widget()

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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
empty($instance)8none
!empty($instance)14wp_get_nav_menu_object()
empty($instance) && $format !== "html5"57–64__(), apply_filters(), current_theme_supports(), apply_filters(), apply_filters(), wp_nav_menu()
!empty($instance) && $format !== "html5"63–70wp_get_nav_menu_object(), __(), apply_filters(), current_theme_supports(), apply_filters(), apply_filters(), wp_nav_menu()
empty($instance) && $format === "html5"69–77__(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), apply_filters(), wp_nav_menu()
!empty($instance) && $format === "html5"75–83wp_get_nav_menu_object(), __(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), apply_filters(), wp_nav_menu()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev908–8271 fewer instruction than PHP 8.5
8.5918–837
8.4918–8372 fewer instructions than PHP 8.3
8.3938–857
8.2938–857
8.1938–8571 fewer instruction than PHP 7.4
7.4948–867

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

3 hooks fire while WP_Nav_Menu_Widget::widget() runs, in this order:

  1. apply_filters( widget_title )filterline 54 (+12 into the body)

    Filters the widget title.

  2. apply_filters( navigation_widgets_format )filterline 72 (+30 into the body)

    Filters the HTML format of widgets with navigation links.

  3. apply_filters( widget_nav_menu_args )filterline 109 (+67 into the body)

    Filters the arguments for the Navigation Menu widget.

Uses · 5

Source code

	public function widget( $args, $instance ) {		// Get menu.		$nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false; 		if ( ! $nav_menu ) {			return;		} 		$default_title = __( 'Menu' );		$title         = ! empty( $instance['title'] ) ? $instance['title'] : ''; 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 		echo $args['before_widget']; 		if ( $title ) {			echo $args['before_title'] . $title . $args['after_title'];		} 		$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 		/**		 * Filters the HTML format of widgets with navigation links.		 *		 * @since 5.5.0		 *		 * @param string $format The type of markup to use in widgets with navigation links.		 *                       Accepts 'html5', 'xhtml'.		 */		$format = apply_filters( 'navigation_widgets_format', $format ); 		if ( 'html5' === $format ) {			// The title may be filtered: Strip out HTML and make sure the aria-label is never empty.			$title      = trim( strip_tags( $title ) );			$aria_label = $title ? $title : $default_title; 			$nav_menu_args = array(				'fallback_cb'          => '',				'menu'                 => $nav_menu,				'container'            => 'nav',				'container_aria_label' => $aria_label,				'items_wrap'           => '<ul id="%1$s" class="%2$s">%3$s</ul>',			);		} else {			$nav_menu_args = array(				'fallback_cb' => '',				'menu'        => $nav_menu,			);		} 		/**		 * Filters the arguments for the Navigation Menu widget.		 *		 * @since 4.2.0		 * @since 4.4.0 Added the `$instance` parameter.		 *		 * @param array   $nav_menu_args {		 *     An array of arguments passed to wp_nav_menu() to retrieve a navigation menu.		 *		 *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.		 *     @type mixed         $menu        Menu ID, slug, or name.		 * }		 * @param WP_Term $nav_menu      Nav menu object for the current menu.		 * @param array   $args          Display arguments for the current widget.		 * @param array   $instance      Array of settings for the current widget.		 */		wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) ); 		echo $args['after_widget'];	}

Changelog

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

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/widgets/class-wp-nav-menu-widget.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.