返回介绍

wp_nav_menu_update_menu_items()

发布于 2017-09-11 12:31:07 字数 4925 浏览 985 评论 0 收藏 0

wp_nav_menu_update_menu_items( int|string $nav_menu_selected_id,  string $nav_menu_selected_title )

Saves nav menu items


description


参数

$nav_menu_selected_id

(int|string) (Required) (id, slug, or name ) of the currently-selected menu

$nav_menu_selected_title

(string) (Required) Title of the currently-selected menu


返回值

(array) $messages The menu updated message


源代码

File: wp-admin/includes/nav-menu.php

function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) {
	$unsorted_menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish' ) );
	$messages = array();
	$menu_items = array();
	// Index menu items by db ID
	foreach ( $unsorted_menu_items as $_item )
		$menu_items[$_item->db_id] = $_item;

	$post_fields = array(
		'menu-item-db-id', 'menu-item-object-id', 'menu-item-object',
		'menu-item-parent-id', 'menu-item-position', 'menu-item-type',
		'menu-item-title', 'menu-item-url', 'menu-item-description',
		'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn'
	);

	wp_defer_term_counting( true );
	// Loop through all the menu items' POST variables
	if ( ! empty( $_POST['menu-item-db-id'] ) ) {
		foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) {

			// Menu item title can't be blank
			if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] )
				continue;

			$args = array();
			foreach ( $post_fields as $field )
				$args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : '';

			$menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );

			if ( is_wp_error( $menu_item_db_id ) ) {
				$messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
			} else {
				unset( $menu_items[ $menu_item_db_id ] );
			}
		}
	}

	// Remove menu items from the menu that weren't in $_POST
	if ( ! empty( $menu_items ) ) {
		foreach ( array_keys( $menu_items ) as $menu_item_id ) {
			if ( is_nav_menu_item( $menu_item_id ) ) {
				wp_delete_post( $menu_item_id );
			}
		}
	}

	// Store 'auto-add' pages.
	$auto_add = ! empty( $_POST['auto-add-pages'] );
	$nav_menu_option = (array) get_option( 'nav_menu_options' );
	if ( ! isset( $nav_menu_option['auto_add'] ) )
		$nav_menu_option['auto_add'] = array();
	if ( $auto_add ) {
		if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) )
			$nav_menu_option['auto_add'][] = $nav_menu_selected_id;
	} else {
		if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) )
			unset( $nav_menu_option['auto_add'][$key] );
	}
	// Remove nonexistent/deleted menus
	$nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
	update_option( 'nav_menu_options', $nav_menu_option );

	wp_defer_term_counting( false );

	/** This action is documented in wp-includes/nav-menu.php */
	do_action( 'wp_update_nav_menu', $nav_menu_selected_id );

	$messages[] = '<div id="message" class="updated notice is-dismissible"><p>' .
		/* translators: %s: nav menu title */
		sprintf( __( '%s has been updated.' ),
			'<strong>' . $nav_menu_selected_title . '</strong>'
		) . '</p></div>';

	unset( $menu_items, $unsorted_menu_items );

	return $messages;
}

更新日志

Versiondescription
3.6.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/taxonomy.php: wp_defer_term_counting()
  • wp-includes/plugin.php: do_action()
  • wp-includes/option.php: update_option()
  • wp-includes/option.php: get_option()
  • wp-includes/post.php: wp_delete_post()
  • wp-includes/nav-menu.php: wp_get_nav_menu_items()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()
  • wp-includes/nav-menu.php: is_nav_menu_item()
  • wp-includes/nav-menu.php: wp_get_nav_menus()
  • wp-includes/nav-menu.php: wp_update_nav_menu
  • wp-includes/load.php: is_wp_error()
  • Show 7 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文