Advertisements

wp_save_nav_menu_items

Definition:
function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() {}

Save posted nav menu item data.

Parameters

  • int $menu_id: The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
  • array $menu_data: The unsanitized posted menu item data.

Return values

returns:The database IDs of the items saved

Source code

function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {

	$menu_id = (int) $menu_id;

	$items_saved = array();



	if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {



		// Loop through all the menu items' POST values

		foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) {

			if (

				empty( $_item_object_data['menu-item-object-id'] ) && // checkbox is not checked

				(

					! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set

					in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default

					! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) ||  // or it's not a custom menu item (but not the custom home page)

					! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists

				)

			) {

				continue; // then this potential menu item is not getting added to this menu

			}



			// if this possible menu item doesn't actually have a menu database ID yet

			if (

				empty( $_item_object_data['menu-item-db-id'] ) ||

				( 0 > $_possible_db_id ) ||

				$_possible_db_id != $_item_object_data['menu-item-db-id']

			) {

				$_actual_db_id = 0;

			} else {

				$_actual_db_id = (int) $_item_object_data['menu-item-db-id'];

			}



			$args = array(

				'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),

				'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),

				'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),

				'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),

				'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),

				'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),

				'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),

				'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),

				'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),

				'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),

				'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),

				'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),

				'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),

			);



			$items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );



		}

	}

	return $items_saved;

}

4081

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: