wp_ajax_add_menu_item()
- Since
- 3.1.0
- Source
wp-admin/includes/ajax-actions.php:1525
Compatibility
- WordPress
- since 3.1.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.
Performance profile
How much work a call to wp_ajax_add_menu_item() 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
- 46–73
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_post().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 155.
Third-party callbacks on 'wp_edit_nav_menu_walker' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly
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 · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_add_menu_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
current_user_can() && !is_wp_error() && $walker_class_name && empty($menu_items) | 46–48 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), wp_die() |
current_user_can() && !is_wp_error() && !$walker_class_name && empty($menu_items) | 49–51 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), wp_die(), wp_die() |
current_user_can() && is_wp_error() && $walker_class_name && empty($menu_items) | 49–51 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), wp_die() |
!current_user_can() && !is_wp_error() && $walker_class_name && empty($menu_items) | 49–51 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), wp_die() |
current_user_can() && is_wp_error() && !$walker_class_name && empty($menu_items) | 52–54 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), wp_die(), wp_die() |
!current_user_can() && !is_wp_error() && !$walker_class_name && empty($menu_items) | 52–54 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), wp_die(), wp_die() |
!current_user_can() && is_wp_error() && $walker_class_name && empty($menu_items) | 52–54 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), wp_die() |
!current_user_can() && is_wp_error() && !$walker_class_name && empty($menu_items) | 55–57 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), wp_die(), wp_die() |
current_user_can() && !is_wp_error() && $walker_class_name && !empty($menu_items) | 62–64 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), after(), walk_nav_menu_tree(), wp_die() |
current_user_can() && !is_wp_error() && !$walker_class_name && !empty($menu_items) | 65–67 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), wp_die(), after(), walk_nav_menu_tree(), wp_die() |
current_user_can() && is_wp_error() && $walker_class_name && !empty($menu_items) | 65–67 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), after(), walk_nav_menu_tree(), wp_die() |
!current_user_can() && !is_wp_error() && $walker_class_name && !empty($menu_items) | 65–67 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), after(), walk_nav_menu_tree(), wp_die() |
4 further outcomes, up to 73 instructions
current_user_can() && is_wp_error() && !$walker_class_name && !empty($menu_items) | 68–70 | check_ajax_referer(), current_user_can(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), wp_die(), after(), walk_nav_menu_tree(), wp_die() |
!current_user_can() && !is_wp_error() && !$walker_class_name && !empty($menu_items) | 68–70 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), apply_filters(), wp_die(), after(), walk_nav_menu_tree(), wp_die() |
!current_user_can() && is_wp_error() && $walker_class_name && !empty($menu_items) | 68–70 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), after(), walk_nav_menu_tree(), wp_die() |
!current_user_can() && is_wp_error() && !$walker_class_name && !empty($menu_items) | 71–73 | check_ajax_referer(), current_user_can(), wp_die(), wp_save_nav_menu_items(), is_wp_error(), wp_die(), apply_filters(), wp_die(), after(), walk_nav_menu_tree(), wp_die() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 154 | 46–73 | 17 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 155 | 46–73 | 17 | |
| 8.4 | 155 | 46–73 | 17 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 157 | 48–75 | 17 | |
| 8.2 | 157 | 48–75 | 17 | 1 more instruction than PHP 8.1 |
| 8.1 | 156 | 48–75 | 17 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 157 | 48–75 | 17 |
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 · 1
One hook fires while wp_ajax_add_menu_item() runs, in this order:
- apply_filters( wp_edit_nav_menu_walker )filterline 1589 (+64 into the body)
Filters the Walker class used when adding nav menu items.
Uses · 12
- check_ajax_referer()Verifies the Ajax request to prevent processing requests external of the blog.
- current_user_can()Returns whether the current user has the specified capability.
- wp_die()Kills WordPress execution and displays HTML page with an error message.
- get_post()Retrieves post data given a post ID or post object.
- get_post_type_object()Retrieves a post type object by name.
- get_term()Gets all term data from database by term ID.
- wp_save_nav_menu_items()Save posted nav menu item data.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_setup_nav_menu_item()Decorates a menu item object with the shared navigation menu item properties.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- walk_nav_menu_tree()Retrieves the HTML list content for nav menu items.
Source code
function wp_ajax_add_menu_item() { check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); if ( ! current_user_can( 'edit_theme_options' ) ) { wp_die( -1 ); } require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; /* * For performance reasons, we omit some object properties from the checklist. * The following is a hacky way to restore them when adding non-custom items. */ $menu_items_data = array(); foreach ( (array) $_POST['menu-item'] as $menu_item_data ) { if ( ! empty( $menu_item_data['menu-item-type'] ) && 'custom' !== $menu_item_data['menu-item-type'] && ! empty( $menu_item_data['menu-item-object-id'] ) ) { switch ( $menu_item_data['menu-item-type'] ) { case 'post_type': $_object = get_post( $menu_item_data['menu-item-object-id'] ); break; case 'post_type_archive': $_object = get_post_type_object( $menu_item_data['menu-item-object'] ); break; case 'taxonomy': $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] ); break; } $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) ); $_menu_item = reset( $_menu_items ); // Restore the missing menu item properties. $menu_item_data['menu-item-description'] = $_menu_item->description; } $menu_items_data[] = $menu_item_data; } $item_ids = wp_save_nav_menu_items( 0, $menu_items_data ); if ( is_wp_error( $item_ids ) ) { wp_die( 0 ); } $menu_items = array(); foreach ( (array) $item_ids as $menu_item_id ) { $menu_obj = get_post( $menu_item_id ); if ( ! empty( $menu_obj->ID ) ) { $menu_obj = wp_setup_nav_menu_item( $menu_obj ); $menu_obj->title = empty( $menu_obj->title ) ? __( 'Menu Item' ) : $menu_obj->title; $menu_obj->label = $menu_obj->title; // Don't show "(pending)" in ajax-added items. $menu_items[] = $menu_obj; } } /** This filter is documented in wp-admin/includes/nav-menu.php */ $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu'] ); if ( ! class_exists( $walker_class_name ) ) { wp_die( 0 ); } if ( ! empty( $menu_items ) ) { $args = array( 'after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new $walker_class_name(), ); echo walk_nav_menu_tree( $menu_items, 0, (object) $args );Changelog
Introduced in 3.1.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-admin/includes/ajax-actions.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.