wppaste
WordPress

WP_Nav_Menu_Widget::form( array $instance )

Since
3.0.0
Source
wp-includes/widgets/class-wp-nav-menu-widget.php:144
Outputs the settings form for the Navigation Menu widget.

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

$instancearray
Current settings.

Performance profile

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

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
94–108

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_Nav_Menu_Widget::form()
  • querycontent queryget_terms()one call below WP_Nav_Menu_Widget::form()

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_Nav_Menu_Widget::form() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$wp_customize instanceof && !($wp_customize instanceof)94–99wp_get_nav_menus(), __(), esc_attr(), printf(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), esc_attr(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), _e()
!($wp_customize instanceof)96–101wp_get_nav_menus(), admin_url(), __(), esc_attr(), printf(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), esc_attr(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), _e()
$wp_customize instanceof101–106wp_get_nav_menus(), __(), esc_attr(), printf(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), esc_attr(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), _e(), _e()
!($wp_customize instanceof) && $wp_customize instanceof103–108wp_get_nav_menus(), admin_url(), __(), esc_attr(), printf(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), esc_attr(), ->get_field_id(), _e(), ->get_field_id(), ->get_field_name(), _e(), _e()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13694–1088
8.513694–1088
8.413694–1088
8.313694–1088
8.213694–1088
8.113694–10882 fewer instructions than PHP 7.4
7.413894–1108

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.

Uses · 9

  • wp_get_nav_menus()Returns all navigation menu objects.
  • admin_url()Retrieves the URL to the admin area for the current site.
  • __()Retrieves the translation of $text.
  • esc_attr()Escaping for HTML attributes.
  • _e()Displays translated text.
  • selected()Outputs the HTML selected attribute.
  • esc_html()Escaping for HTML blocks.
  • WP_Nav_Menu_Widget::get_field_id()
  • WP_Nav_Menu_Widget::get_field_name()

Source code

	public function form( $instance ) {		global $wp_customize;		$title    = isset( $instance['title'] ) ? $instance['title'] : '';		$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : ''; 		// Get menus.		$menus = wp_get_nav_menus(); 		$empty_menus_style     = '';		$not_empty_menus_style = '';		if ( empty( $menus ) ) {			$empty_menus_style = ' style="display:none" ';		} else {			$not_empty_menus_style = ' style="display:none" ';		} 		$nav_menu_style = '';		if ( ! $nav_menu ) {			$nav_menu_style = 'display: none;';		} 		// If no menus exists, direct the user to go and create some.		?>		<p class="nav-menu-widget-no-menus-message" <?php echo $not_empty_menus_style; ?>>			<?php			if ( $wp_customize instanceof WP_Customize_Manager ) {				$url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';			} else {				$url = admin_url( 'nav-menus.php' );			} 			printf(				/* translators: %s: URL to create a new menu. */				__( 'No menus have been created yet. <a href="%s">Create some</a>.' ),				// The URL can be a `javascript:` link, so esc_attr() is used here instead of esc_url().				esc_attr( $url )			);			?>		</p>		<div class="nav-menu-widget-form-controls" <?php echo $empty_menus_style; ?>>			<p>				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>				<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />			</p>			<p>				<label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>				<select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">					<option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>					<?php foreach ( $menus as $menu ) : ?>						<option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>							<?php echo esc_html( $menu->name ); ?>						</option>					<?php endforeach; ?>				</select>			</p>			<?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>				<p class="edit-selected-nav-menu" style="<?php echo $nav_menu_style; ?>">					<button type="button" class="button"><?php _e( 'Edit Menu' ); ?></button>				</p>			<?php endif; ?>		</div>		<?php	}

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