wppaste
WordPress

wp_ajax_add_menu_item()

Since
3.1.0
Source
wp-admin/includes/ajax-actions.php:1545
Handles adding a menu item via AJAX.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
46–73

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

Plugin surface
1 hook

Third-party callbacks on 'wp_edit_nav_menu_walker' 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

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
current_user_can() && !is_wp_error() && $walker_class_name && empty($menu_items)46–48check_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–51check_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–51check_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–51check_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–54check_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–54check_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–54check_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–57check_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–64check_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–67check_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–67check_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–67check_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–70check_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–70check_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–70check_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–73check_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

PHPCompiledExecutedBranchesNotes
8.6-dev15446–73171 fewer instruction than PHP 8.5
8.515546–7317
8.415546–73172 fewer instructions than PHP 8.3
8.315748–7517
8.215748–75171 more instruction than PHP 8.1
8.115648–75171 fewer instruction than PHP 7.4
7.415748–7517

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:

  1. apply_filters( wp_edit_nav_menu_walker )filterline 1609 (+64 into the body)

    Filters the Walker class used when adding nav menu items.

Uses · 12

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_object = get_post( $menu_item_id ); 		if ( ! empty( $menu_object->ID ) ) {			$menu_object        = wp_setup_nav_menu_item( $menu_object );			$menu_object->title = empty( $menu_object->title ) ? __( 'Menu Item' ) : $menu_object->title;			$menu_object->label = $menu_object->title; // Don't show "(pending)" in ajax-added items.			$menu_items[]       = $menu_object;		}	} 	/** 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.

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