Updating Nav Menu Items with AJAX problem


On my latest project, I'm having a difficult time updating our main menu because it has many items on it. I have tried php post_max_vars = 10000 or even greater with no success.
The menu cannot be saved completely. So I decided to use AJAX in order to save menu items one by one.

I have nearly accomplished this task by modifying someone's code that I came upon today, with a slight problem. Now, I can change menu item URL's and titles, but each time I cannot seem to set the menu item's menu order. Here is the PHP code in my functions file:

function rudr_include_menu_script() {
  wp_enqueue_script( 'livequery', get_stylesheet_directory_uri() . '/lib/js/livequery.js', array('jquery'), null, false );
  wp_enqueue_script( 'menuitems', get_stylesheet_directory_uri() . '/lib/js/menuitems.js', array('jquery','livequery'), null, false );

add_action( 'admin_enqueue_scripts', 'rudr_include_menu_script' );

function rudr_saveitem_callback(){

  // parameters for wp_insert_post() function (yes, menu elements are the custom post types)
  $my_postarr = array(
    'ID'    => $_POST['menuid'],
    'post_title'    => $_POST['title'],
    'post_type' => 'nav_menu_item',
    'post_status' => 'publish',
    //'menu_order'  => intval(stripslashes($_POST['pos'])), // I comment this line because the code doesn't work properly with menu item position
    'post_content'  => $_POST['desc'], // description
    'post_excerpt'   => $_POST['attrtitle'] // title attribute

  // we should set the right position for new menu items
  if ( 'draft' == get_post_status( intval($_POST['menuid']) ) ) {
    $my_postarr['menu_order'] = $_POST['max'];

  // insert or update the menu element
  $my_post_id = wp_insert_post( $my_postarr );

  // set the right menu for it (yes, menus are taxonomies)
  wp_set_object_terms( $my_post_id, intval($_POST['menu']), 'nav_menu', $append = false );

  // post meta parameters
  $target = ($_POST['target'] == 'checked') ? '_blank' : '';
  update_post_meta( $my_post_id, '_menu_item_target', $target);
  update_post_meta( $my_post_id, '_menu_item_menu_item_parent', $_POST['parentid']);
  update_post_meta( $my_post_id, '_menu_item_classes', $_POST['classes']);
  update_post_meta( $my_post_id, '_menu_item_xfn', esc_attr($_POST['xfn']));
  update_post_meta( $my_post_id, '_menu_item_url', esc_attr($_POST['url']));

  if ( intval( $_POST['pos'] )!= get_post_field( 'menu_order', $my_post_id ) ) {
    $my_post_2 = array(
      'ID'    => intval($_POST['menuid']),
      'post_type' => 'nav_menu_item',
      'menu_order' => intval(stripslashes($_POST['pos'])),

    $my_post_id2 = wp_update_post( $my_post_2 );


if( is_admin() ) {
  add_action('wp_ajax_saveitem', 'rudr_saveitem_callback');

And here is the javascript to modify nav menu item with AJAX:


	$(".menu-item-settings").livequery(function() {
		$(this).append('<a style="display: block; float: left;" class="button button-primary rudr-menu-save">Güncelle</a>');
		$(this).append('<form style="display: inline-block; float: left;">&nbsp;S?ralama:&nbsp;<input type="number" class="menu-item-data-position-changeable" value=' + $(this).parent().find('.menu-item-data-position').val() +'></form>');

	// if you do not want to use Live Query plugin, then comment the lines above and uncomment the lines below
	//	$(this).append('<a class="button button-primary rudr-menu-save">Edit</a>');

		button = $(this);
		el = button.parent();
		var url = el.find('.edit-menu-item-url').val();
		var attrtitle = el.find('.edit-menu-item-attr-title').val();
		var title = el.find('.edit-menu-item-title').val();
		var classes = el.find('.edit-menu-item-classes').val();
		var xfn = el.find('.edit-menu-item-xfn').val();
		var desc = el.find('.edit-menu-item-description').val();
		var target = el.find('.field-link-target').find('input').attr('checked');
		var menuid =  el.find('.menu-item-data-db-id').val();
		var parentid = el.find('.menu-item-data-parent-id').val();
		var menu = $('#menu').val();
		var pos = el.find('.menu-item-data-position-changeable').val();
			type: 'POST',
			url: ajaxurl,
			data: 'action=saveitem&url=' + url + '&attrtitle=' + attrtitle + '&title=' + title + '&classes=' + classes + '&xfn=' + xfn + '&desc=' + desc + '&target=' + target + '&menuid=' + menuid + '&parentid=' + parentid + '&menu=' + menu + '&pos=' + pos + '&max=' + $('.menu-item-settings').length,
			beforeSend: function(xhr){
			success: function(data){
				button.text('Güncellendi ' + pos);
		return false;


		button = $(this);
		delid = button.attr('id');
		ajaxdelurl = button.attr('href');
		var itemID = parseInt(button.attr('id').replace('delete-', ''), 10);
			type: 'GET',
			url: ajaxdelurl,
			beforeSend: function(xhr){
			success: function(data){
		return false;


Since I'm developing this on a local machine, I cannot give you admin access. However you can replicate this issue on your own local server too. Also, I'm using Max Mega Menu plugin but turning it off did not have any effect on this problem.

Thanks for your support.