WP_Nav_Menu_Widget::form( array $instance )
- Since
- 3.0.0
- Source
wp-includes/widgets/class-wp-nav-menu-widget.php:144
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
- Scaling
- Scales with input
- Instructions
- 94–108
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_terms().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 136.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()one call below WP_Nav_Menu_Widget::form() - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
$wp_customize instanceof && !($wp_customize instanceof) | 94–99 | wp_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–101 | wp_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 instanceof | 101–106 | wp_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 instanceof | 103–108 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 136 | 94–108 | 8 | |
| 8.5 | 136 | 94–108 | 8 | |
| 8.4 | 136 | 94–108 | 8 | |
| 8.3 | 136 | 94–108 | 8 | |
| 8.2 | 136 | 94–108 | 8 | |
| 8.1 | 136 | 94–108 | 8 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 138 | 94–110 | 8 |
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( '— Select —' ); ?></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.
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.